当前位置: 代码迷 >> Sql Server >> 一个简单的sql语句 解决后马上散分 多谢大家了
  详细解决方案

一个简单的sql语句 解决后马上散分 多谢大家了

热度:231   发布时间:2016-04-27 21:52:43.0
一个简单的sql语句 解决后马上散分 谢谢大家了 在线等
我要做的只有一步   就是让每次循环前   where   [email protected]@i自动+1   可是我这样写   @i的值   送不进去   请问该如何解决?

set   @i=1

declare   cur_tmp1   cursor   for  

select   subj_code,flag,row
                            from   ht_code_flag_table  
where   [email protected]  

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   @[email protected]+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 [email protected]

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 @[email protected]+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 [email protected]

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 [email protected]
while @@rowcount> 0
begin

print @subj_code

set @[email protected]+1



end



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

PRINT @subj_code

SET @[email protected]+1
END
  相关解决方案