先看一下原表:
DECLARE @id INT;
DECLARE @name NVARCHAR(100);
DECLARE c_department CURSOR FOR SELECT StuID, Name FROM dbo.Stu_info;
OPEN c_department; -- 打开游标
FETCH NEXT FROM c_department
INTO @id,
@name;
WHILE @@FETCH_STATUS = 0 --返回被 FETCH语句执行的最后游标的状态--
BEGIN
PRINT '学号' + CONVERT(VARCHAR(10), @id) + ':' + (@name);
FETCH NEXT FROM c_department
INTO @id,
@name; -- 转到下一个游标
END;
CLOSE c_department; -- 关闭游标
DEALLOCATE c_department; -- 释放游标
运行结果: