閱讀632 返回首頁    go 技術社區[雲棲]


SQL遊標的使用

table1結構如下
 id    int
 name  varchar(50)
 
 declare @id int
 declare @name varchar(50)
 declare cursor1 cursor for         --定義遊標cursor1
 select * from table1               --使用遊標的對象(跟據需要填入select文)
 open cursor1                       --打開遊標
 
 fetch next from cursor1 into @id,@name  --將遊標向下移1行,獲取的數據放入之前定義的變量@id,@name中
 
 while @@fetch_status=0           --判斷是否成功獲取數據
 begin
 update table1 set name=name+'1'
 where id=@id                           --進行相應處理(跟據需要填入SQL文)
 
 fetch next from cursor1 into @id,@name  --將遊標向下移1行
 end
 
 close cursor1                   --關閉遊標
 deallocate cursor1

最後更新:2017-04-02 16:47:54

  上一篇:go 移動互聯網產品設計的原則
  下一篇:go MQ命令學習總結大全MQ常用命令