自己写的一个使用游标的小例子
游标因效率低下而为人所诟病,但当我们做很多“一次性”的工作的时候,游标还是很方便地,嘿嘿。刚刚用游标解决了一个实际的问题,记录在下,以后备查,也给想用游标的同志提供一个小例子(注释都删掉了哦,大家关注里面的语法就行了):
1
DECLARE product_cursor CURSOR FOR
2
SELECT
3
VendeeClassID, UserID, VendeeClassCode
4
FROM
5
VendeeClass order by UserID
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
declare @UserID int
8
declare @ClassCode varchar(8)
9
declare @VendeeClassID int
10![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
declare @globalUserID int
12
declare @globalCount int
13
declare @globalClassCode varchar(8)
14
set @globalUserID = -1
15![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
OPEN product_cursor
17
FETCH NEXT FROM product_cursor INTO @VendeeClassID, @UserID, @ClassCode
18
WHILE @@FETCH_STATUS = 0
19
BEGIN
20
if(@globalUserID = @UserID)
21
begin
22
set @globalCount = @globalCount+1
23
if(@globalCount>9)
24
begin
25
set @globalClassCode = '01' + cast(@globalCount as varchar)
26
end
27
else
28
begin
29
set @globalClassCode = '010' + cast(@globalCount as varchar)
30
end
31
if(@ClassCode is null)
32
update VendeeClass set VendeeClassCode=@globalClassCode where VendeeClassID=@VendeeClassID
33
end
34
else
35
begin
36
set @globalUserID = @UserID
37
set @globalCount = 1;
38
if(@ClassCode is null)
39
update VendeeClass set VendeeClassCode='0101' where VendeeClassID=@VendeeClassID
40
end
41![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
FETCH NEXT FROM product_cursor INTO @VendeeClassID, @UserID, @ClassCode
43
END
44![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
CLOSE product_cursor
46
DEALLOCATE product_cursor
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)