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变量中)
END
CLOSE 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
END
select @id1=@id2,@t1=@t2
END
close cur
DEALLOCATE cur