SQLServer 游标 循环 示例

Declare @tmp1 varchar(255), @tmp2 varchar(50);
declare @tmp3 int, @tmp4 int, @tmp5 int;
Declare MyCursor Cursor
for Select distinct tmp1, tmp2 From table1 order by tmp2
Open MyCursor
Fetch next From MyCursor 
Into @tmp1,@tmp2
while(@@fetch_status=0)
  begin
    begin
      if not exists(select * from table2 where tmp1 = @tmp1 and tmp2 = @tmp2)
        begin
         exec get_systemkey 'table2Key', @tmp3 output
         --Group1
         if(@tmp1 = 'Poor')
         select @tmp4 = 51, @tmp5 = 5;
         if(@tmp1 = 'Fair')
         select @tmp4 = 41, @tmp5 = 2;
         if(@tmp1 = 'Good')
         select @tmp4 = 31, @tmp5 = 3;
         if(@tmp1 = 'Very good')
         select @tmp4 = 21, @tmp5 = 4;
         if(@tmp1 = 'Excellent')
         select @tmp4 = 11, @tmp5 = 5;

         insert into table2(tmp1, tmp2, tmp3, tmp4, tmp5) values (@tmp1, @tmp3, @tmp2, @tmp4, 1, @tmp5);
      end
    end
  fetch next From MyCursor Into @tmp1,@tmp2
end
Close MyCursor
Deallocate MyCursor
posted @ 2020-08-17 12:51  nil  阅读(219)  评论(0编辑  收藏  举报