日期:2014-05-19  浏览次数:20384 次

一个简单的sql语句 解决后马上散分 谢谢大家了 在线等
我要做的只有一步   就是让每次循环前   where   row=@i中的@i自动+1   可是我这样写   @i的值   送不进去   请问该如何解决?

set   @i=1

declare   cur_tmp1   cursor   for  

select   subj_code,flag,row
                            from   ht_code_flag_table  
where   row=@i  

open   cur_tmp1
fetch   next   from   cur_tmp1   into   @subj_code,@flag,@row
while   (@@fetch_status   =0)

begin

print   @subj_code  

fetch   next   from   cur_tmp1   into   @subj_code,@flag,@row
set   @i=@i+1



end
close   cur_tmp1
deallocate   cur_tmp1

------解决方案--------------------
set @i=1

while @i <=MAX..
BEGIN
declare cur_tmp1 cursor for
select subj_code,flag,row
from ht_code_flag_table
where row=@i

open cur_tmp1
fetch next from cur_tmp1 into @subj_code,@flag,@row
while (@@fetch_status =0)

begin

print @subj_code

fetch next from cur_tmp1 into @subj_code,@flag,@row
set @i=@i+1



end
close cur_tmp1
deallocate cur_tmp1
END
------解决方案--------------------
declare @row int
set @i=1

select @row = max(row) from ht_code_flag_table

while @row > = @i
begin
declare cur_tmp1 cursor for
select subj_code,flag,row
from ht_code_flag_table
where row=@i

open cur_tmp1
fetch next from cur_tmp1 into @subj_code,@flag,@row
while (@@fetch_status =0)

begin
print @subj_code
fetch next from cur_tmp1 into @subj_code,@flag,@row
end
close cur_tmp1
deallocate cur_tmp1
set @i = @i + 1
end
------解决方案--------------------
这样改吧

set @i=1

select @subj_code=subj_code,@flag=flag,@row=row
from ht_code_flag_table
where row=@i
while @@rowcount> 0
begin

print @subj_code

set @i=@i+1



end



------解决方案--------------------
--lz想取得从1开始的连续subj_code,这个需求不需要用游标
SET @i=1
WHILE EXISTS (SELECT 1 FROM ht_code_flag_TABLE WHERE row=@i)
BEGIN
SELECT @subj_code=subj_code
FROM ht_code_flag_table
WHERE row=@i

PRINT @subj_code

SET @i=@i+1
END