sql 游标例子 根据一表的数据去筛选另一表的数据

时间:2022-07-10 18:04:45

sql 游标例子 根据一表的数据去筛选另一表的数据

DECLARE @MID nvarchar(20)
DECLARE @UTime datetime

DECLARE @TBL_Temp table
(
MID nvarchar(20),
Uptime datetime,
Chat nvarchar(20)
)

--Get User
DECLARE My_Cursor CURSOR --定义游标
FOR ( SELECT distinct MID, Uptime from TLinkTest)

OPEN My_Cursor

fetch next from My_Cursor INTO @MID,@UTime

while (@@fetch_status=0)
BEGIN

INSERT INTO @TBL_Temp SELECT TOP 2 * FROM TLinkContent WHERE MID = @MID AND Uptime < @UTime

fetch next from My_Cursor INTO @MID,@UTime

END

close My_Cursor

deallocate My_Cursor

SELECT * FROM @TBL_Temp