SQL中用遊標迴圈更新資料(原)

來源:互聯網
上載者:User

     在實際項目中,有時候會用到資料得更新,我最近就遇到了。我有一個產品表(product),其中有三個欄位是外鍵對象的ID,可是現在需要做搜尋了,所以存ID就不行了,要在產品的關鍵字裡面儲存這三個外鍵ID的Name屬性,也就是要儲存物件的名稱而不是ID,這樣便於抓取資料(本人使用的Lucene,對純文字抓取比較強悍)。

    那麼現在問題很清楚了,就是要逐條更行產品表裡面的每一條記錄。我的關鍵字的定義規格是:“產品名稱,第一類型名稱,第二類型名稱,第三類型名稱”。也就是說,需要對每一行進行這樣一個操作:首頁根據三個外鍵ID查詢出對應的Name名稱,然後把三個Name和產品名稱根據規則連在一起,再更新這條資料的關鍵字列。

   好了,那思路很清楚了,做一件事,然後迴圈就可以了,那就要先做這件事,也就是建立一個預存程序,來完成第一步。

 

View Code  1 use 資料庫名稱
 2 go
 3 if exists(select * from sysobjects where name = 'updateKeyword')
 4 drop procedure updateKeyword
 5 go
 6 create proc updateKeyword
 7 @productID varchar(50)
 8 
 9 as
10 declare @typeID varchar(50)
11 declare @typeID2 varchar(50)
12 declare @typeID3 varchar(50)
13 declare @typeName varchar(50)
14 declare @typeName2 varchar(50)
15 declare @typeName3 varchar(50)
16 if exists (select * from manager_product where ID = @productID)
17 begin
18 select @typeID = productFirstType_ID,@typeID2 = productSecondType_ID,@typeID3 = productThirdType_ID 
19 from manager_product where ID = @productID
20 print 'typeIDs show : '+@typeID+','+@typeID2+','+@typeID3
21 end
22 
23 
24 if(@typeID is null)
25 begin
26     select @typeName = ''
27 end
28 else
29 begin
30    if exists (select * from manager_producttype where ID = @typeID)
31      begin
32      select @typeName = typeName from manager_producttype where ID = @typeID
33      print 'cunzai typeID : ' +  @typeID +',typeName :'+ @typeName
34      end
35 
36 end
37 
38 
39 if(@typeID2 is null)
40 begin
41     select @typeName2 = ''
42 end
43 else
44 begin
45     if exists (select * from manager_producttype where ID = @typeID2)
46     begin
47     select @typeName2 = typeName from manager_producttype where ID = @typeID2
48     print 'cunzai typeID2 : ' +  @typeID2 +',typeName2 :'+ @typeName2
49     end
50 end
51 
52 if(@typeID3 is null)
53 begin
54     select @typeName3 = ''
55 end
56 else
57 begin
58     if exists (select * from manager_producttype where ID = @typeID3)
59     begin
60     select @typeName3 = typeName from manager_producttype where ID = @typeID3
61     print 'cunzai typeID3 : ' +  @typeID3 +',typeName3 :'+ @typeName3
62     end
63 end
64 print 'updateString : '+ @typeName + @typeName2 + @typeName3
65 update manager_product set  productKeyword = productName + ',' + @typeName +','+@typeName2+','+@typeName3
66 where ID = @productID

 好了,第一步經過測試,ok了。基本上完成了需求,可以實現:更新一條產品資訊,將其關鍵字更新成需要的規則了。

    現在進行第二步,就是迴圈這個操作,對產品表的每一條資料進行更新。這裡我選用了遊標來完成。

 1 declare @productID nvarchar(50)
 2 declare cursorOfID cursor for
 3   select ID from manager_product
 4 --開啟遊標
 5 open cursorOfID
 6 --擷取資料,遊標下移一行
 7 fetch next from cursorOfID into @productID  
 8 --檢測擷取資料是否成功
 9 while @@fetch_status=0
10 begin
11   --顯示通過遊標賦值的變數
12   exec updateKeyword @productID --執行
13   --遊標繼續下移
14   fetch next from cursorOfID into @productID  
15 end
16 --關閉遊標
17 close cursorOfID

 在第12行,調用了寫好的預存程序來完成更新,每次更新完,讓遊標往下讀一行,將新值賦給@productID,這樣就實現了迴圈更新資料了。

測試,通過。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.