当前位置: 代码迷 >> Sql Server >> sql server 游标!该如何解决
  详细解决方案

sql server 游标!该如何解决

热度:434   发布时间:2016-04-24 18:34:22.0
sql server 游标!
本人对游标不懂,所以在这求点游标的例子(从易到难带注释),我自己来做练习.


------解决方案--------------------
create table table1
(KHMC varchar(20), SPDM varchar(10), DJ varchar(10), SL int, XSSL int)

insert into table1
 select '广西骆俊峰', '5609B', '100.0000', 12, null union all
 select '广西骆俊峰', '5609B', '80.0000', 7, null union all
 select '广西骆俊峰', '5609B', '60.0000', 6, null union all
 select '广西骆俊峰', '5609B', '50.0000', 13, null union all
 select '广西骆俊峰', '5609B', '40.0000', 21, null


create table table2
(khmc varchar(20), spdm varchar(10), sl int, bysl int)

insert into table2
 select '广西骆俊峰', '5609B', 20, null
 


declare @table1_khmc varchar(10)
declare @table1_spdm varchar(20)
declare @table1_sl int
declare @table1_xssl int

--定义table1的游标
declare cur_table1 cursor
for select KHMC,SPDM,sl,xssl from table1 for update  --可以游标更新的


declare @table2_khmc varchar(10)
declare @table2_spdm varchar(20)
declare @table2_sl int


--定义table2的游标
declare cur_table2 cursor
for select khmc,spdm,sl from table2   --用于查询的游标
 

open cur_table2;  --打开游标

--从游标中取数,放到变量中
fetch next from cur_table2 into @table2_khmc,@table2_spdm,@table2_sl


while @@FETCH_STATUS = 0   --外层游标cur_table2的遍历
begin
   open cur_table1;
   fetch next from cur_table1 into @table1_khmc,@table1_spdm,@table1_sl,@table1_xssl
   
   while @@FETCH_STATUS = 0  --内存游标cur_table1的遍历
   begin
      if (@table1_khmc = @table2_khmc) and (@table2_spdm = @table1_spdm)
      begin
update table1
set xssl = case when @table2_sl >= isnull(@table1_sl,0)
                     then @table1_sl
                when @table2_sl < isnull(@table1_sl,0)
                     then @table2_sl
           end
where current of cur_table1;

--如果table2的sl大于table1的sl,那么可以继续循环,否则就退出内层有游标
if @table2_sl >= isnull(@table1_sl,0)
   set @table2_sl = @table2_sl - ISNULL(@table1_sl,0);
else
   break;
   
fetch next from cur_table1 into @table1_khmc,@table1_spdm,@table1_sl,@table1_xssl
  end
  
   end
   
   close cur_table1; --关闭内层游标
   
   fetch next from cur_table2 into @table2_khmc,@table2_spdm,@table2_sl
   
end

close cur_table2;   --关闭游标

deallocate cur_table2;  --释放游标cur_table2的资源

deallocate cur_table1;  --释放游标cur_table1的资源


--查询更新后的结果
select *
from table1
/*
KHMC     SPDM DJ       SL XSSL
广西骆俊峰 5609B 100.0000  12 12
广西骆俊峰 5609B 80.0000   7     7
广西骆俊峰 5609B 60.0000   6     1
广西骆俊峰 5609B 50.0000   13 NULL
广西骆俊峰 5609B 40.0000   21 NULL
*/

------解决方案--------------------
上面的例子稍微有点难度,是2层游标,实现了比较复杂的逻辑。

看看,这个是完整的例子:

游标
http://blog.csdn.net/sqlserverdiscovery/article/details/12657219


------解决方案--------------------
这是一个相对简单的例子,就是输出每天记录的值:

create table table1
(KHMC varchar(20), SPDM varchar(10), DJ varchar(10), SL int, XSSL int)
 
insert into table1
 select '广西骆俊峰', '5609B', '100.0000', 12, null union all
 select '广西骆俊峰', '5609B', '80.0000', 7, null union all
 select '广西骆俊峰', '5609B', '60.0000', 6, null union all
 select '广西骆俊峰', '5609B', '50.0000', 13, null union all
 select '广西骆俊峰', '5609B', '40.0000', 21, null
 go
 
 
 
declare cur_table1 cursor
for select * from table1

declare @KHMC varchar(20)
declare @SPDM varchar(10)
declare @DJ varchar(10)
  相关解决方案