mssql游标入门
1、 定义游标:declare cursor_name cursor
For select 语句;
2、 打开游标:open cursor_name
3、 循环访问游标中的每一行数据:
Fetch next from cursor_name into @参数列表
4、 游标的状态:@@fetch_status,用于判断游标fetch的状态,当为0时正常,不为一时异常
5、关闭并释放资源
例:
declare @whcId nchar(5),@whc int;declare whc_cursor cursorfor select CustomerID,EmployeeID from dbo.Ordersopen whc_cursorfetch next from whc_cursor into @whcId,@whcwhile @@fetch_status = 0beginprint @whcid+' '+convert(nchar(5),@whc)fetch next from whc_cursor into @whcId,@whcendclose whc_cursordeallocate whc_cursor |

浙公网安备 33010602011771号