在 SQL Server 2008 R2 中使用游标更新
我想更新名为 Employeekopie1
的特定表中的列。
我尝试更新的列是 FK_Profiel
(值的类型为 int
)
我尝试放入 FK_Profiel
列中的值是这些值我正在得到 从光标。游标从不同表中的列获取值,使用联接来获取正确的值。
使用的选择查询的结果返回具有不同值的多行。
select查询的第一个结果是114,这是正确的。问题是这个值被分配给FK_Profiel
列中的所有字段,这不是我的本意。
我想分配选择查询中的所有值。
代码如下:
DECLARE @l_profiel int;
DECLARE c1 CURSOR
FOR select p.ProfielID
from DIM_Profiel p,DIM_EmployeeKopie1 e1,employee e
where e1.EmpidOrigineel = e.emplid and e.profile_code = p.Prof_Code
for update of e1.FK_Profiel;
open c1;
FETCH NEXT FROM c1 into @l_profiel
WHILE @@FETCH_STATUS = 0
BEGIN
SET NOCOUNT ON;
UPDATE DIM_EmployeeKopie1
set FK_Profiel = @l_profiel
where current of c1
end
close c1;
deallocate c1;
请帮忙,thx。
I want to update a column in a specific table called Employeekopie1
.
The column I am trying to update is FK_Profiel
(values are type int
)
The values I am trying to put in the column FK_Profiel
are the values I am getting
from a cursor. The cursor is getting values from a column in a different table, using joins to get the correct values.
The result of the select query used returns multiple rows with different values.
The first result of the select query is 114, which is correct. The problem is that this value is assigned to all the fields in the column FK_Profiel
, which is not my intention.
I want to assign all the values from the select query.
The code is as follows:
DECLARE @l_profiel int;
DECLARE c1 CURSOR
FOR select p.ProfielID
from DIM_Profiel p,DIM_EmployeeKopie1 e1,employee e
where e1.EmpidOrigineel = e.emplid and e.profile_code = p.Prof_Code
for update of e1.FK_Profiel;
open c1;
FETCH NEXT FROM c1 into @l_profiel
WHILE @@FETCH_STATUS = 0
BEGIN
SET NOCOUNT ON;
UPDATE DIM_EmployeeKopie1
set FK_Profiel = @l_profiel
where current of c1
end
close c1;
deallocate c1;
Please help, thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您忘记将 FETCH NEXT 添加到循环中。
但您根本不需要光标。
试试这个:
You forgot to add
FETCH NEXT
into the loop.But you don't need a cursor for this at all.
Try this:
首先,您不需要
CURSOR
为此,您可以在没有它的情况下执行UPDATE
。而且您还应该使用显式的JOINS
而不是隐式的。请尝试以下操作:First af all, you don't need a
CURSOR
for this, you can do anUPDATE
without it. And you also should use explicitJOINS
instead of implicit ones. Try the following: