不断出现“光标是只读的”
我的代码看起来很先进。
我想使用唯一的计数器(不等于 {1,2,3,...})更新特定字段。
我不断收到错误“光标为只读”。
另外:有没有更简单的方法?
declare @MaxVal int = NULL
declare @fetchVal int = NULL
select @MaxVal = MAX(tp_Id)+1 from [<tableContainingInitialMaxval>]
/** some default **/
DECLARE curs01 CURSOR
for select @maxVal + row_number() OVER (order by [<someUniqueField>]) from [<table2update>];
(used FOR UPDATE OF [<field2update>] but that made no difference)
open curs01
FETCH NEXT FROM curs01 INTO @fetchVal;
WHILE @@FETCH_STATUS = 0
begin
update [<table2update>] set [<field2update>] = @fetchVal
WHERE CURRENT OF curs01;
FETCH NEXT FROM curs01 INTO @fetchVal;
end;
CLOSE curs01;
DEALLOCATE curs01;
GO
My code seems pretty forward.
I want to update a specific field with a unique counter, not equal {1,2,3,...}.
I keep getting the error 'The cursor is READ ONLY.'
Also: is there a simpler way?
declare @MaxVal int = NULL
declare @fetchVal int = NULL
select @MaxVal = MAX(tp_Id)+1 from [<tableContainingInitialMaxval>]
/** some default **/
DECLARE curs01 CURSOR
for select @maxVal + row_number() OVER (order by [<someUniqueField>]) from [<table2update>];
(used FOR UPDATE OF [<field2update>] but that made no difference)
open curs01
FETCH NEXT FROM curs01 INTO @fetchVal;
WHILE @@FETCH_STATUS = 0
begin
update [<table2update>] set [<field2update>] = @fetchVal
WHERE CURRENT OF curs01;
FETCH NEXT FROM curs01 INTO @fetchVal;
end;
CLOSE curs01;
DEALLOCATE curs01;
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您不需要光标。
You don't need a cursor for this.