当前位置: 代码迷 >> Sql Server >> 游标有关问题,为什么会取不到数据呢
  详细解决方案

游标有关问题,为什么会取不到数据呢

热度:89   发布时间:2016-04-27 11:32:53.0
游标问题,为什么会取不到数据呢?
SQL code
declare ee scroll cursorforselect company,team,client from t group by company,team,clientopen eedeclare @rows int,@rand int set @rows=(select count(1) from (select company,team,client from t group by company,team,client) b)declare @company nvarchar(30),@team nvarchar(30),@client nvarchar(30)while(@@Fetch_Status=0)beginselect @rand=cast( floor(rand()*100) as int)while(@rand>0)beginFetch next from ee into @company,@team,@clientinsert ss (company,team,client) values(@company,@team,@client)select @[email protected]endendclose eedeallocate eeselect * from ss


其中语句"select company,team,client from t group by company,team,client
"能查出279条记录,第一次运行的时候能取到数据,但之后每次提取的时候,ss都是没有数据的,我刚刚学游标,不懂这是什么原因,请各位大哥赐教。

------解决方案--------------------
SQL code
-->trydeclare ee scroll cursorforselect company,team,client from t group by company,team,clientopen eedeclare @rows int,@rand int select @rows=count(1) from t group by company,team,clientdeclare @company nvarchar(30),@team nvarchar(30),@client nvarchar(30)Fetch from ee into @company,@team,@clientwhile(@@Fetch_Status=0)begin    select @rand=cast( floor(rand()*100) as int)    while(@rand>0)    begin        Fetch next from ee into @company,@team,@client        insert ss (company,team,client) values(@company,@team,@client)        select @[email protected]    endendclose eedeallocate eeselect * from ss
------解决方案--------------------
你监控一下这个值,会不会是第二次就为0了?select @[email protected]
------解决方案--------------------
没对游标操作 怎么上来就 @@Fetch_Status
------解决方案--------------------
一楼 正解
------解决方案--------------------


在while前面在一行

Fetch next from ee into @company,@team,@client

while(@@Fetch_Status=0)


  相关解决方案