[People] Transact-SQL cursor quickly forgotten

Original: [people] Transact-SQL cursor quickly forgotten


When first learning SQL Server, when learned cursor, suddenly had a kind of intimacy, because through this a while loop, a one way to deal with data, much like the process of learned language, and C language very similar.

Slowly, some a little bit complicated SQL, they tend to think of using the cursor to deal with. It is also because of extensive use, gradually, during use, but also really appreciate the cursor inefficient, a one approach, a small amount of data in a case also, when a lot of data once, the process when they are very complex and often speed will be very slow.


Ever since, slowly began to abandon the use of the cursor, and finally almost forgotten this old friend.

Today, suddenly I discovered that someone in the forum asked a question, hope to solve with the cursor, thus, let me once again reminded of the cursor.


Here is the problem raised by users:



I have two tables, namely: table1
 KHMC SPDM DJ SL XSSL
Guangxi Luo Junfeng 5609B 100.0000 12 NULL
Guangxi Luo Junfeng 5609B 80.0000 7 NULL
Guangxi Luo Junfeng 5609B 60.0000 6 NULL
Guangxi Luo Junfeng 5609B 50.0000 13 NULL
Guangxi Luo Junfeng 21 40.0000 NULL 5609B
table2
khmc SPDM SL bysl
Guangxi Luo Junfeng 5609B 20 NULL

and the result I want is to go with the second data table filled with rows of a table of data (association is khmc, SPDM):
 kHMC SPDM DJ SL XSSL
Guangxi Luo Junfeng 5609B 100.0000 12 12
Guangxi Luojun Feng 5609B 80.0000 7 7
Guangxi Luojun Feng 5609B 60.0000 6 1
Guangxi Luojun Feng 5609B 50.0000 13 NULL
Guangxi Luojun Feng 5609B 40.0000 21 NULL


The first is the construction of the table statement:


   
   
  1. create table table1
  2. (KHMC varchar( 20), SPDM varchar( 10), DJ varchar( 10), SL int, XSSL int)
  3. insert into table1
  4. select '广西骆俊峰', '5609B', '100.0000', 12, null union all
  5. select '广西骆俊峰', '5609B', '80.0000', 7, null union all
  6. select '广西骆俊峰', '5609B', '60.0000', 6, null union all
  7. select '广西骆俊峰', '5609B', '50.0000', 13, null union all
  8. select '广西骆俊峰', '5609B', '40.0000', 21, null
  9. create table table2
  10. (khmc varchar( 20), spdm varchar( 10), sl int, bysl int)
  11. insert into table2
  12. select '广西骆俊峰', '5609B', 20, null

Then the code is a cursor, the cursor is nested within the outer cursor and a cursor:


   
   
  1. declare @table1_khmc varchar( 10)
  2. declare @table1_spdm varchar( 20)
  3. declare @table1_sl int
  4. declare @table1_xssl int
  5. --定义table1的游标
  6. declare cur_table1 cursor
  7. for select KHMC,SPDM,sl,xssl from table1 for update --可以游标更新的
  8. declare @table2_khmc varchar( 10)
  9. declare @table2_spdm varchar( 20)
  10. declare @table2_sl int
  11. --定义table2的游标
  12. declare cur_table2 cursor
  13. for select khmc,spdm,sl from table2 --用于查询的游标
  14. open cur_table2; --打开游标
  15. --从游标中取数,放到变量中
  16. fetch next from cur_table2 into @table2_khmc,@table2_spdm,@table2_sl
  17. while @@FETCH_STATUS = 0 --外层游标cur_table2的遍历
  18. begin
  19. open cur_table1;
  20. fetch next from cur_table1 into @table1_khmc,@table1_spdm,@table1_sl,@table1_xssl
  21. while @@FETCH_STATUS = 0 --内存游标cur_table1的遍历
  22. begin
  23. if (@table1_khmc = @table2_khmc) and (@table2_spdm = @table1_spdm)
  24. begin
  25. update table1
  26. set xssl = case when @table2_sl >= isnull(@table1_sl, 0)
  27. then @table1_sl
  28. when @table2_sl < isnull(@table1_sl, 0)
  29. then @table2_sl
  30. end
  31. where current of cur_table1;
  32. --如果table2的sl大于table1的sl,那么可以继续循环,否则就退出内层有游标
  33. if @table2_sl >= isnull(@table1_sl,0)
  34. set @table2_sl = @table2_sl - ISNULL(@table1_sl, 0);
  35. else
  36. break;
  37. fetch next from cur_table1 into @table1_khmc,@table1_spdm,@table1_sl,@table1_xssl
  38. end
  39. end
  40. close cur_table1; --关闭内层游标
  41. fetch next from cur_table2 into @table2_khmc,@table2_spdm,@table2_sl
  42. end
  43. close cur_table2; --关闭游标
  44. deallocate cur_table2; --释放游标cur_table2的资源
  45. deallocate cur_table1; --释放游标cur_table1的资源
  46. --查询更新后的结果
  47. select *
  48. from table1
  49. /*
  50. KHMC SPDM DJ SL XSSL
  51. 广西骆俊峰 5609B 100.0000 12 12
  52. 广西骆俊峰 5609B 80.0000 7 7
  53. 广西骆俊峰 5609B 60.0000 6 1
  54. 广西骆俊峰 5609B 50.0000 13 NULL
  55. 广西骆俊峰 5609B 40.0000 21 NULL
  56. */


Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12019966.html