游標的簡單應用

2021-09-24 15:35:44 字數 928 閱讀 4883

使用游標前資料

declare @loginid varchar(50)

declare @loginpass varchar(50)

declare cursor1 cursor for   --定義游標cursor1

select loginid,loginpassword from users where loginid<54 ----使用游標的物件(跟據需要填入select表)

open cursor1      --開啟游標

fetch next from cursor1 into @loginid,@loginpass  --將游標向下移1行,獲取的資料放入之前定義的變數@bankid ,@name,@rmbnum 中

while @@fetch_status=0           --判斷是否成功獲取資料

begin

update users set loginpassword=@loginpass+1

where loginid=@loginid                           --進行相應處理(跟據需要填入sql文)

fetch next from cursor1 into  @loginid,@loginpass  --將游標向下移1行,獲取的資料放入之前定義的變數@bankid ,@name,@rmbnum 中

endclose cursor1                   --關閉游標

deallocate cursor1   --刪除游標與游標名稱或游標變數之間的關聯。

使用後的資料

mssql游標的簡單應用

游標修改資料 declare alarm cursor cursor global scroll for select lsc id,station id,alarm time,clear time,start time,end time from tmptelemetertwoday open a...

ORACLE游標的應用

在oracle資料庫中,可以使用游標瀏覽資料 更新資料和刪除資料,接下來列舉以幾個簡單的例子 通過使用游標既可以逐行檢索結果集中的記錄,又可以更新或刪除當前游標行的資料如果要通過游標更新或刪除資料,在定義游標時必須要帶有for update子句其語句格式如下 cursor cursor name i...

游標的簡單使用

create table tablea tid int not null,mid int not null go insert into tablea values 2,2 insert into tablea values 3,3 insert into tablea values 4,4 ins...