奇怪的调用参数的存储过程?
存储过程:
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




