CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

奇怪的调用参数的存储过程?

楼主binbare(学习·学习·再学习!)2002-09-12 08:51:04 在 MS-SQL Server / 基础类 提问

存储过程:  
   
  CREATE   procedure   search_query_splitpage  
  @Key   nvarchar(200)   ,  
  @CurrentPage   int,  
  @PageCount   int,  
  @RowCount   int   OUTPUT  
  as  
   
  declare   @SelectStr   nvarchar(1000)  
  declare   @TimeName   nvarchar(25)  
  declare   @key_string   nvarchar(220)  
   
  set   @TimeName   =   convert(nvarchar(23),   getdate(),   121)  
  set   @TimeName   =   REPLACE(@TimeName,   '.',   '')  
  set   @TimeName   =   REPLACE(@TimeName,   ':',   '')  
  set   @TimeName   =   REPLACE(@TimeName,   '-',   '')  
  set   @TimeName   =   REPLACE(@TimeName,   '   ',   '')  
  set   @key_string='%'   +   @Key   +   '%'  
  select   @RowCount=count(id)   from   linking   where   sitename   like   @key_string   or   jiji   like   @key_string  
   
  if   @RowCount   >   0  
   
  begin  
   
  exec('create   table   ##Tab'   +   @TimeName   +   '   (  
  id   int   identity,  
  sitename   nvarchar(200),  
  jiji   nvarchar(200),  
  url   nvarchar(200),  
  fenleiid   int  
  )'  
  )  
   
  exec('insert   ##Tab'   +   @TimeName   +'(sitename,jiji,url,fenleiid)   select   sitename,jiji,url,fenleiid   from   linking   where   sitename   like   '''   +   @key_string   +   '''   or   jiji   like   '''   +   @key_string   +   '''   order   by   power   desc')  
  exec('select   *   from   ##Tab'   +   @TimeName   +   '   where   id   between   (('   +   @CurrentPage   +   '-1)*'   +   @PageCount   +   '+1)   and   '   +   @CurrentPage   +   '*'   +   @PageCount   +   '   order   by   id')  
  exec('drop   table   ##Tab'   +   @TimeName)  
   
  end  
  GO  
   
  调用代码:  
  <%  
  adCmdSPStoredProc   =   4  
  adParamReturnValue   =   4  
  adParaminput   =   1  
  adParamOutput   =   2  
  adInteger   =   3  
  adVarChar   =   200  
   
  iVal   =   5  
  oVal   =   3  
   
  Set   conn=server.createobject("ADODB.CONNECTION")  
  connstr="Driver={SQL   Server};Description=sqldemo;SERVER=(local);uid=sa;pwd=;DATABASE=cxh"  
  conn.open   connstr    
   
  set   cmdTemp=Server.Createobject("Adodb.Command")  
  Set   cmdTemp.ActiveConnection=Conn  
  cmdTemp.CommandType=adCmdSPStoredProc  
  cmdTemp.CommandText="search_query_splitpage"  
   
  cmdTemp.Parameters.Append   cmdTemp.CreateParameter("@Key",adVarChar,adParamInput,200,"Key")  
  cmdTemp.Parameters.Append   cmdTemp.CreateParameter("@CurrentPage",adInteger,adParamInput,,iVal)  
  cmdTemp.Parameters.Append   cmdTemp.CreateParameter("@PageCount",adInteger,adParamInput,,iVal)  
  cmdTemp.Parameters.Append   cmdTemp.CreateParameter("@RowCount",adInteger,adParamOutput,,oVal)  
   
  Maxlist=30  
  currentpage=request.form("page")  
  if   isNumeric(currentpage)=0   then   currentpage=1  
  currentpage=Cint(currentpage)  
  if   currentpage<1   then   currentpage=1  
  key=trim(request.form("key"))  
   
  cmdTemp("@Key")=key  
  cmdTemp("@CurrentPage")=currentpage  
  cmdTemp("@PageCount")=Maxlist  
  Set   Rs   =   cmdTemp.Execute  
   
  RowCount=cmdTemp("@RowCount")  
  Response.Write   cmdTemp.Parameters("@RowCount").Value  
   
  if   RowCount>0   then  
   
  While   Not   RS.EOF  
   
  for   each   Field   in   RS.Fields  
  Response.Write   Field.Name   &   "="   &   Field.Value   &   "<br>"   &   vbCRLF  
  Next  
  Response.Write   "<br>"  
  RS.MoveNext  
  Wend  
   
  end   if  
   
  Set   Rs=Nothing  
  Set   cmdTemp.ActiveConnection=Nothing  
  Set   cmdTemp=Nothing  
  Conn.Close  
  Set   Conn=Nothing  
  %>  
   
  上面的代码在sql2000已经测试通过,但有个小问题。就是如果我把要取得记录集:Set   Rs   =   cmdTemp.Execute   后,就不能取得返回集:用cmdTemp("@RowCount")和cmdTemp.Parameters("@RowCount").Value都不行。如果不取得记录集,直接用cmdTemp.Execute,就可以取得返回值:用cmdTemp("@RowCount")和cmdTemp.Parameters("@RowCount").Value都可以。  
   
  百思不得其解。望高手们帮忙。  
   
  还有以上的存储过程在sql   server   7.0通不过。错误是7.0里不支持"*"这个乘号,不知有没有办法解决?  
   
  问题点数:100、回复次数:9Top

1 楼saucer(思归)回复于 2002-09-12 08:58:30 得分 100

you   have   to   close   the   recordset   before   retrieving   the   output   value  
   
  Set   Rs   =   cmdTemp.Execute  
  if   RowCount>0   then  
   
  While   Not   RS.EOF  
   
  for   each   Field   in   RS.Fields  
  Response.Write   Field.Name   &   "="   &   Field.Value   &   "<br>"   &   vbCRLF  
  Next  
  Response.Write   "<br>"  
  RS.MoveNext  
  Wend  
   
  end   if  
   
  RS.Close  
  RowCount=cmdTemp("@RowCount")  
  Response.Write   cmdTemp.Parameters("@RowCount").Value  
  Top

2 楼binbare(学习·学习·再学习!)回复于 2002-09-12 09:18:35 得分 0

不会吧?要先关了记录集才能取得返回值?我是要取得这个返回值后再按他的结果来显示记录集的呀?该如何解决这个问题呀?Top

3 楼saucer(思归)回复于 2002-09-12 09:29:00 得分 0

1.   do   you   really   need   rowcount??   you   can   try   like   this:  
   
  Set   Rs   =   cmdTemp.Execute  
  While   Not   RS.EOF  
   
  for   each   Field   in   RS.Fields  
  Response.Write   Field.Name   &   "="   &   Field.Value   &   "<br>"   &   vbCRLF  
  Next  
  Response.Write   "<br>"  
  RS.MoveNext  
  Wend  
  RS.Close  
   
   
  2.   if   you   insist,   you   can   change   your   stored   procedure   like   this:  
   
  select   @RowCount=count(id)   from   linking   where   sitename   like   @key_string   or   jiji   like   @key_string  
   
  select   @RowCount   as   RowCount  
   
  .....  
   
  then   inside   your   asp,   you   can   try  
  ....  
   
  Set   Rs   =   cmdTemp.Execute  
  RowCount   =   RS("RowCount")  
  set   RS   =RS.NextRecordset  
  if   RowCount>0   then  
  While   Not   RS.EOF  
  for   each   Field   in   RS.Fields  
  Response.Write   Field.Name   &   "="   &   Field.Value   &   "<br>"   &   vbCRLF  
  Next  
  Response.Write   "<br>"  
  RS.MoveNext  
  Wend  
  end   if  
  RS.CloseTop

4 楼binbare(学习·学习·再学习!)回复于 2002-09-12 09:41:08 得分 0

返回行数是一定需要的。我知道在这里可以省略。是画蛇添足。但我这个行数的用处不在这。主要是拿来做分页用的。  
   
  你的下面这段改的思路很清晰。我能理解。你的意思是先把@rowcount先存成一条记录显示,然后先取得这个记录,再向下移动游标再进行显示。  
   
  不过我试了再存储过程里加上select   @RowCount   as   RowCount,提示出错。不知这句该如何加?  
   
  不过我自己也想到了好的方法:  
   
  Set   Rs   =   cmdTemp.Execute  
  GetString   =   Rs.Getrows()  
  Rs.Close  
  RowCount=cmdTemp("@RowCount")  
   
  把记录集先保存个一个数组里,再进行处理,呵呵。  
   
  不过还是要谢谢你,希望继续以上的问题,怎么以你的理路做?不一定采用,但多学学别人的思路是好的。  
   
  还有你能不能解决在7.0里以上的存储过程出错,不能用"*"乘号运算符?Top

5 楼yxsalj(想和你去吹吹风)回复于 2002-09-12 09:42:23 得分 0

CREATE   procedure   search_query_splitpage  
  @Key   nvarchar(200)   ,  
  @CurrentPage   int,  
  @PageCount   int,  
  @RowCount   int   OUTPUT  
  as  
  set   nocount   on  
  ...  
  exec('drop   table   ##Tab'   +   @TimeName)  
  set   oncount   off  
   
  Top

6 楼binbare(学习·学习·再学习!)回复于 2002-09-12 09:53:41 得分 0

上面的。请指教set   nocount的意思和用法?Top

7 楼binbare(学习·学习·再学习!)回复于 2002-09-12 09:58:22 得分 0

我按你上面的加set   nocunt   on   提示语法错误!Top

8 楼gzhughie(hughie)回复于 2002-09-12 10:00:40 得分 0

你的这段有错误  
  cmdTemp.Parameters.Append   cmdTemp.CreateParameter("@Key",adVarChar,adParamInput,200,"Key")  
  cmdTemp.Parameters.Append   cmdTemp.CreateParameter("@CurrentPage",adInteger,adParamInput,,iVal)  
  cmdTemp.Parameters.Append   cmdTemp.CreateParameter("@PageCount",adInteger,adParamInput,,iVal)  
  cmdTemp.Parameters.Append   cmdTemp.CreateParameter("@RowCount",adInteger,adParamOutput,,oVal)  
   
  存储过程默认的第一个参数应该是return参数,你在这里没有定义他,所以你取不会返回纪录集,而你直接执行存储过程时,ado帮你做了处理,所以可以执行。Top

9 楼binbare(学习·学习·再学习!)回复于 2002-09-12 10:10:19 得分 0

上面的你说的不对。我也曾用过用return取得返回值。也行不通的。  
   
  你看清楚我的问题了。我现在是记录集和返回值不能同时取得的问题  
   
  当关了记录集就可以了。Top

相关问题

  • 存储过程,参数调用的问题,奇怪的很!!!
  • 带参数的存储过程调用
  • 调用带参数的存储过程
  • 怎么调用带参数的存储过程? (内附存储过程)
  • 在VB中如何用调用带参数的存储过程?
  • ASP调用存储过程参数问题
  • command对象使用参数调用存储过程的问题
  • 在VB中如何调用带参数的存储过程?
  • 如何用command调用存储过程(输入、输出参数)?
  • 调用Smalldatetime型参数的存储过程的问题

关键词

  • 存储过程
  • 解决
  • cmdtemp
  • timename
  • rowcount
  • jiji
  • eoffor
  • fieldsresponse
  • vbcrlfnextresponse
  • rs

得分解答快速导航

  • 帖主:binbare
  • saucer

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo