SQL存储过程中使用游标对查询结果循环
有时候需要在SQL中对查询结果循环,下面是一个使用游标的示例:
1set ANSI_NULLS ON
2
3set QUOTED_IDENTIFIER ON
4
5GO
6
7ALTER PROCEDURE [dbo].[GetProductPriceFromYear]
8
9 @ProductID int,
10
11 @Date Datetime
12
13AS
14
15BEGIN
16
17 create table #temp1
18
19(
20
21 id int IDENTITY(1,1) NOT NULL primary key,
22
23 AddDate varchar(20),
24
25 [datename] varchar(20),
26
27 week varchar(20),
28
29 ProductName varchar(20),
30
31 ProductID varchar(20),
32
33 LPrice float(20),
34
35 HPrice float(20),
36
37 APrice float(20),
38
39 WeekLPrice float(20),
40
41 WeekHPrice float(20),
42
43 WeekAPrice float(20),
44
45 WeekDayCount int
46
47)
48
49
50
51declare @tempDate varchar(20)
52
53declare @AddDate varchar(20)
54
55declare @datenam varchar(20)
56
57declare @week varchar(20)
58
59declare @ProductName varchar(20)
60
61declare @Product varchar(20)
62
63declare @LPrice varchar(20)
64
65declare @HPrice varchar(20)
66
67declare @APrice varchar(20)
68
69Declare CurPrice Cursor For
70
71 select distinct AddDate=CONVERT(char(10),a.AddDate,20),datename(dw,a.AddDate) as [datename],
72
73datepart(ww,a.AddDate) as week, b.ProductName,a.ProductID,a.LPrice,a.HPrice,a.APrice
74
75from dbo.SmsPrice as a join dbo.SmsProduct as b
76
77on a.ProductID = b.ProductId where a.ProductID = @ProductID AND Datediff(yy,a.AddDate,@Date)=0 order by AddDate desc
78
79Open CurPrice
80
81Fetch from CurPrice into @AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice
82
83insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice)
84
85 values(@AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice)
86
87
88
89While @@FETCH_STATUS=0
90
91 BEGIN
92
93
94
95 if(@tempDate!=@AddDate)
96
97 insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice)
98
99 values(@AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice)
100
101 set @tempDate = @AddDate
102
103 Fetch from CurPrice into @AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice
104
105 end
106
107 Close CurPrice
108
109 Deallocate CurPrice
110
111
112
113--insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice)
114
115--select distinct AddDate=CONVERT(char(10),a.AddDate,20),datename(dw,a.AddDate) as [datename],
116
117--datepart(ww,a.AddDate) as week, b.ProductName,a.ProductID,a.LPrice,a.HPrice,a.APrice
118
119--from dbo.Price as a join dbo.SmsProduct as b
120
121--on a.ProductID = b.ProductId where a.ProductID = @ProductID AND Datediff(yy,a.AddDate,@Date)=0 order by AddDate desc
122
123declare @weekid varchar(20)
124
125declare @tempWeek2 varchar(20)
126
127declare @WeekLPrice float(20)
128
129declare @WeekHPrice float(20)
130
131declare @WeekAPrice float(20)
132
133
134
135Declare Cur Cursor For
136
137 select distinct week from #temp1
138
139Open Cur
140
141 Fetch Cur Into @weekid
142
143While @@FETCH_STATUS=0
144
145 BEGIN
146
147 select @tempWeek2=week,@WeekLPrice=avg(CONVERT(float,LPrice)),@WeekHPrice=avg(CONVERT(float,HPrice)),
148
149 @WeekAPrice=avg(CONVERT(float,APrice)) from #temp1 where week =@weekid group by week
150
151 update #temp1 set WeekLPrice = @WeekLPrice,WeekHPrice=@WeekHPrice,WeekAPrice=@WeekAPrice,
152
153 WeekDayCount=(select count(*) from #temp1 where week = @weekid) where week = @weekid
154
155 Fetch Cur Into @weekid
156
157 end
158
159 Close Cur
160
161 Deallocate cur
162
163select * from #temp1
164
165END
166
2
3set QUOTED_IDENTIFIER ON
4
5GO
6
7ALTER PROCEDURE [dbo].[GetProductPriceFromYear]
8
9 @ProductID int,
10
11 @Date Datetime
12
13AS
14
15BEGIN
16
17 create table #temp1
18
19(
20
21 id int IDENTITY(1,1) NOT NULL primary key,
22
23 AddDate varchar(20),
24
25 [datename] varchar(20),
26
27 week varchar(20),
28
29 ProductName varchar(20),
30
31 ProductID varchar(20),
32
33 LPrice float(20),
34
35 HPrice float(20),
36
37 APrice float(20),
38
39 WeekLPrice float(20),
40
41 WeekHPrice float(20),
42
43 WeekAPrice float(20),
44
45 WeekDayCount int
46
47)
48
49
50
51declare @tempDate varchar(20)
52
53declare @AddDate varchar(20)
54
55declare @datenam varchar(20)
56
57declare @week varchar(20)
58
59declare @ProductName varchar(20)
60
61declare @Product varchar(20)
62
63declare @LPrice varchar(20)
64
65declare @HPrice varchar(20)
66
67declare @APrice varchar(20)
68
69Declare CurPrice Cursor For
70
71 select distinct AddDate=CONVERT(char(10),a.AddDate,20),datename(dw,a.AddDate) as [datename],
72
73datepart(ww,a.AddDate) as week, b.ProductName,a.ProductID,a.LPrice,a.HPrice,a.APrice
74
75from dbo.SmsPrice as a join dbo.SmsProduct as b
76
77on a.ProductID = b.ProductId where a.ProductID = @ProductID AND Datediff(yy,a.AddDate,@Date)=0 order by AddDate desc
78
79Open CurPrice
80
81Fetch from CurPrice into @AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice
82
83insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice)
84
85 values(@AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice)
86
87
88
89While @@FETCH_STATUS=0
90
91 BEGIN
92
93
94
95 if(@tempDate!=@AddDate)
96
97 insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice)
98
99 values(@AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice)
100
101 set @tempDate = @AddDate
102
103 Fetch from CurPrice into @AddDate,@datenam,@week,@ProductName,@Product,@LPrice,@HPrice,@APrice
104
105 end
106
107 Close CurPrice
108
109 Deallocate CurPrice
110
111
112
113--insert into #temp1(AddDate,[datename],week,ProductName,ProductID,LPrice,HPrice,APrice)
114
115--select distinct AddDate=CONVERT(char(10),a.AddDate,20),datename(dw,a.AddDate) as [datename],
116
117--datepart(ww,a.AddDate) as week, b.ProductName,a.ProductID,a.LPrice,a.HPrice,a.APrice
118
119--from dbo.Price as a join dbo.SmsProduct as b
120
121--on a.ProductID = b.ProductId where a.ProductID = @ProductID AND Datediff(yy,a.AddDate,@Date)=0 order by AddDate desc
122
123declare @weekid varchar(20)
124
125declare @tempWeek2 varchar(20)
126
127declare @WeekLPrice float(20)
128
129declare @WeekHPrice float(20)
130
131declare @WeekAPrice float(20)
132
133
134
135Declare Cur Cursor For
136
137 select distinct week from #temp1
138
139Open Cur
140
141 Fetch Cur Into @weekid
142
143While @@FETCH_STATUS=0
144
145 BEGIN
146
147 select @tempWeek2=week,@WeekLPrice=avg(CONVERT(float,LPrice)),@WeekHPrice=avg(CONVERT(float,HPrice)),
148
149 @WeekAPrice=avg(CONVERT(float,APrice)) from #temp1 where week =@weekid group by week
150
151 update #temp1 set WeekLPrice = @WeekLPrice,WeekHPrice=@WeekHPrice,WeekAPrice=@WeekAPrice,
152
153 WeekDayCount=(select count(*) from #temp1 where week = @weekid) where week = @weekid
154
155 Fetch Cur Into @weekid
156
157 end
158
159 Close Cur
160
161 Deallocate cur
162
163select * from #temp1
164
165END
166