declare @userid varchar(50)
declare my_cursor cursor --定義游標
for (select userid from dbo.memberaccount) --查出需要的集合放到游標中
open my_cursor; --開啟游標
fetch next from my_cursor into @userid; --讀取第一行資料(將memberaccount表中的userid放到@userid變數中)
while @@fetch_status = 0
begin
print @userid; --列印資料(列印memberaccount表中的userid)
update dbo.memberservice set servicetime = dateadd(month, 6, getdate()) where userid = @userid; --更新資料
fetch next from my_cursor into @userid; --讀取下一行資料(將memberaccount表中的userid放到@userid變數中)
endclose my_cursor; --關閉游標
deallocate my_cursor; --釋放游標
go
上面是搜到學習的,下面是根據上面學習寫的查詢相鄰資料時間間隔大於3分鐘的資料,下面是**
declare @id1 int,@t1 datetime,@t2 datetime,@id2 int
declare cur cursor for select businessdataid,openedtime from dbo.tmptime where openedtime between '2016-02-17 07:00' and '2016-02-17 08:00' order by openedtime
open cur
fetch next from cur into @id1,@t1
while @@fetch_status=0
begin
fetch next from cur into @id2,@t2
if (datediff(s, @t1, @t2)>=180)
begin
select * from dbo.tmptime where businessdataid=@id2
endselect @id1=@id2,@t1=@t2
endclose cur
deallocate cur
sql游標迴圈
declare begindate datetime convert datetime,2019.05.06 09 40 50 enddate datetime getdate idint declare my cursor cursor 定義游標 for select id from dbo.op...
SQL游標迴圈
begin 定義的這些變數就是以下查出的結果的字段數,一一對應 declare cid int,cno nvarchar 64 cname nvarchar 256 debtamount decimal begin 定義游標.declare vernier main cursor fast forw...
sql 游標迴圈遍歷
原文 sql 游標迴圈遍歷 寫儲存過程的時候碰到乙個需要對資料進行遍歷迴圈操作的問題,最後通過游標解決了,感覺很適用 1 declare level varchar 100 2 declare uid varchar 100 3 declare cur cursor 定義乙個游標 4read onl...