USE Northwind
GO
--声明一个游标
DECLARE toolbar_cursor CURSOR FOR
SELECT ProductID,ProductName,CategoryID
FROM Products where CategoryID=2
--打开游标
OPEN toolbar_cursor
--定义接收游标属性的变量
declare @ProductID int
declare @ProductName nvarchar(40)
declare @CategoryID int
--从游标提取数据
--从游标得到一行数据使用FETCH命令。每一次提取数据后,游标都指向结果集的下一行。语法如下:
--FETCH cursor_name INTO variable[,variable,...]
--对于SELECT定义的游标的每一列,FETCH变量列表都应该有一个变量与之相对应,变量的类型也要相同。
--把游标当前值赋值给变量方法一
--FETCH toolbar_cursor INTO @ProductID,@ProductName,@CategoryID
--把游标当前值赋值给变量方法二
--FETCH NEXT FROM toolbar_cursor INTO @ProductID,@ProductName,@CategoryID