如何解决光标永远重复第一条记录
有人可以告诉我我的代码有什么问题吗?我只是想遍历一个有 2 条记录的表并让它返回 2 条记录。但正如您在下图中所看到的,它一直重复第一条记录(永远,直到我点击取消)。谢谢
SET NOCOUNT ON -- Improves performance by not returning number of rows affected
--General Variables
DECLARE @ImportGUID uniqueidentifier =NEWID() -- Declares and sets a new Unique number. Can be used to remove records at a later stage
--Cursor Variables
DECLARE @FirstNameVariable varchar(50)
DECLARE @SurnameVariable varchar(50)
PRINT 'Starting import ' + CONVERT(varchar(255),@ImportGUID); --just display this on the screen
--Declare the first cursor which will loop through a table collecting data
DECLARE NewPersonTableImportCursor CURSOR FOR
SELECT Firstname,Surname
from dbo.A_NewPersonTable
--Open NewPersonTableImportCursor
OPEN NewPersonTableImportCursor
--Start looping through the data and updating the cursor variables with data from this cursor
FETCH NEXT FROM NewPersonTableImportCursor INTO @FirstNameVariable,@SurnameVariable
WHILE @@FETCH_STATUS=0 --Fetch Status 0 means successfull so only proceed on those rows from source table that were successfull
BEGIN
PRINT @FirstNameVariable;
END
CLOSE NewPersonTableImportCursor
解决方法
在循环结束前添加FETCH NEXT FROM NewPersonTableImportCursor INTO @FirstNameVariable,@SurnameVariable
以获取下一条记录
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。