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:
-
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
Then the code is a cursor, the cursor is nested within the outer cursor and a cursor:
-
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
-
*/