日期:2014-05-18  浏览次数:20429 次

一个简单的存储过程返回记录集的问题--只返回一条记录
存储过程
@newsclid   新闻id
@newstopnum   返回记录数
---------------------------------------
CREATE   proc   wu_news_top
@newsclid   int,
@newstopnum   int
as
begin
exec( 'select   top   '+@newstopnum+ '     news_id,news_title,news_content   from   wu_news   where   ncl_id= '+@newsclid+ '   order   by   news_id   desc ')
end
GO

asp调用
____________________________________________________________
set   rs=server.createobject( "adodb.recordset ")
strsql= "wu_news_top   18,3 "
rs.open   strsql,conn,1,1
if   not   rs.eof   then
dim   ni
ni=0
do   while   not   rs.eof
%>
..............................
<%
rs.movenext
ni=ni+1
if   ni> =rs.recordcount   then   exit   do  
loop
end   if
rs.close
set   rs=nothing
%>

问题:
1、为什么只返回一条记录?
2、修改
exec( 'select   top   '+@newstopnum+ '     news_id,news_title,news_content   from   wu_news   where   ncl_id= '+@newsclid+ '   order   by   news_id   desc ')

set   @strs= 'select   top   '+@newstopnum+ '     news_id,news_title,news_content   from   wu_news   where   ncl_id= '+@newsclid+ '   order   by   news_id   desc '
exec(@strs)
asp程序报错,为什么?

请给出正确的使用方法,谢谢!

------解决方案--------------------
估计是你的rs.recordcount 有问题,可能是-1吧,修改rs.open的参数吧
------解决方案--------------------
2、修改
exec( 'select top '+@newstopnum+ ' news_id,news_title,news_content from wu_news where ncl_id= '+@newsclid+ ' order by news_id desc ')

set @strs= 'select top '+@newstopnum+ ' news_id,news_title,news_content from wu_news where ncl_id= '+@newsclid+ ' order by news_id desc '
exec(@strs)

----------
這一步要這麼修改


Declare @strs varchar(1000)
set @strs= 'select top '+Cast(@newstopnum As Varchar)+ ' news_id,news_title,news_content from wu_news where ncl_id= '+Cast(@newsclid As Varchar)+ ' order by news_id desc '
exec(@strs)
------解决方案--------------------
CREATE proc wu_news_top
@newsclid int,
@newstopnum int
as
begin
declare @sql varchar(1000)
set @sql= 'select top '+cast(@newstopnum as varchar)+ ' news_id,news_title,news_content from wu_news where ncl_id= '+cast(@newsclid as varchar)+ ' order by news_id desc '
exec(@sql)
end
GO

------解决方案--------------------
那是刚开始的时候 rs.recordcount=-1造成的;
加上
rs.CursorLocation = 3
rs.open strsql,conn,1,1
rs.recordcount就不会有问题了,就是等于记录总数!