SQL存储过程中使用游标对查询结果循环

有时候需要在SQL中对查询结果循环,下面是一个使用游标的示例:

 

  1 set  ANSI_NULLS  ON
  2
  3 set  QUOTED_IDENTIFIER  ON
  4
  5 GO
  6
  7 ALTER   PROCEDURE   [ dbo ] . [ GetProductPriceFromYear ]  
  8
  9         @ProductID   int ,
 10
 11         @Date   Datetime
 12
 13 AS
 14
 15 BEGIN
 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
 51 declare   @tempDate   varchar ( 20 )
 52
 53 declare   @AddDate   varchar ( 20 )
 54
 55 declare   @datenam   varchar ( 20 )
 56
 57 declare   @week   varchar ( 20 )
 58
 59 declare   @ProductName   varchar ( 20 )
 60
 61 declare   @Product   varchar ( 20 )
 62
 63 declare   @LPrice   varchar ( 20 )
 64
 65 declare   @HPrice   varchar ( 20 )
 66
 67 declare   @APrice   varchar ( 20 )
 68
 69 Declare    CurPrice    Cursor     For  
 70
 71                select   distinct  AddDate = CONVERT ( char ( 10 ),a.AddDate, 20 ), datename (dw,a.AddDate)  as   [ datename ] ,
 72
 73 datepart (ww,a.AddDate)  as  week, b.ProductName,a.ProductID,a.LPrice,a.HPrice,a.APrice 
 74
 75 from  dbo.SmsPrice  as  a  join  dbo.SmsProduct  as  b 
 76
 77 on  a.ProductID  =  b.ProductId  where  a.ProductID  =   @ProductID   AND   Datediff (yy,a.AddDate, @Date ) = 0   order   by  AddDate  desc
 78
 79 Open    CurPrice     
 80
 81 Fetch    from  CurPrice  into   @AddDate , @datenam , @week , @ProductName , @Product , @LPrice , @HPrice , @APrice
 82
 83 insert   into  #temp1(AddDate, [ datename ] ,week,ProductName,ProductID,LPrice,HPrice,APrice)
 84
 85                       values ( @AddDate , @datenam , @week , @ProductName , @Product , @LPrice , @HPrice , @APrice )
 86
 87  
 88
 89 While     @@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
123 declare   @weekid   varchar ( 20 )
124
125 declare   @tempWeek2   varchar ( 20 )
126
127 declare   @WeekLPrice   float ( 20 )
128
129 declare   @WeekHPrice   float ( 20 )
130
131 declare   @WeekAPrice   float ( 20 )
132
133  
134
135 Declare    Cur    Cursor     For  
136
137                select   distinct  week  from  #temp1
138
139 Open    Cur     
140
141    Fetch    Cur    Into     @weekid
142
143 While     @@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
163 select   *   from  #temp1
164
165 END
166

你可能感兴趣的:(存储过程)