游标提供了对每条记录进行不相同的更新的方法,通过指针的位置,更改不同的记录。
注:游标名称不需要以 @ 开头。
DECLARE cursor_name CURSOR
FOR SELECT ... FROM table
DECLARE @id int, @str varchar(1000), @dt datetime
OPEN cursor_name
FETCH cursor_name INTO @id,@str,@dt WHILE @@FETCH_STATUS = 0 BEGIN IF @id=0 UPDATE table SET id=100 WHERE CURRENT OF cursor_name FETCH NEXT FROM cursor_name INTO @id,@str,@dt END
CLOSE cursor_name DEALLOCATE cursor_name |