51代码网ORACLEMYSQLSQL SERVER其它数据库java/jspasp/asp.netC/C++/VC++APP应用其它语言服务器应用
您现在的位置: 51代码网 >> sqlserver >> 文章正文

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

更新时间:2012-8-15:  来源:51代码网

declare ee scroll cursor
for
select company,team,client from t group by company,team,client
open ee
declare @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)
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 @rand=@rand-1
end
end
close ee
deallocate ee

select * from ss

 

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

SQL code
-->try
declare ee scroll cursor
for
select company,team,client from t group by company,team,client
open ee
declare @rows int,@rand int
select @rows=count(1) from t group by company,team,client
declare @company nvarchar(30),@team nvarchar(30),@client nvarchar(30)
Fetch from ee into @company,@team,@client
while(@@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 @rand=@rand-1
    end
end
close ee
deallocate ee

select * from ss
在while前面在一行

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

while(@@Fetch_Status=0)

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 赞助商链接
    推荐文章
  • 此栏目下没有推荐文章
  • {
    设为首页 | 加入收藏 | 友情链接 | 网站地图 | 联系站长 |