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 cursor for select CustomerID,EmployeeID from dbo.Orders open whc_cursor fetch next from whc_cursor into @whcId,@whc while @@fetch_status = 0 begin print @whcid+ ' ' + convert ( nchar (5),@whc) fetch next from whc_cursor into @whcId,@whc end close whc_cursor deallocate whc_cursor |