我需要更新超过1000条记录。这些值在另一个表中。
我尝试了这个查询:
Update tblEnrollment
SET TrOOPCurrentYrBalanceAmt = tr.TrOOPCurrentYrBalanceAmt
from tblTrOOPbalance tr
join tblMember mem on tr.MedicareNumber = mem.MANumber
join tblEnrollment enr on mem.MemberID = enr.MemberID
where enr.EnrollmentID IN ('16823', '16828')
但是我收到了错误:
Msg 512,Level 16,State 1,Procedure tblEnrollment_UpdateTrigger,Line 9
子查询返回的值超过1 当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
我想我无法使用查询更新超过1个。如何更新多条记录?
谢谢,
因为表tblenrollment有一个阻止更新多个记录的触发器,所以我必须使用LOOP逐个更新记录。 谁能帮忙怎么写LOOP查询呢?
答案 0 :(得分:2)
对于cource,你可以更新多个记录!
您的过程tblEnrollment_UpdateTrigger
中似乎有其他代码,并且子查询存在问题。请检查您是否有类似情况,其中sabquery将返回超过1条记录:
where column = (select column2 from table)
update tab set column = (select column2 from table)
select @variable = (select column2 from table)
关于UPDATE
声明,我认为最好使用类似的东西:
update enr
set enr.TrOOPCurrentYrBalanceAmt = tr.TrOOPCurrentYrBalanceAmt
from tblEnrollment enr
join tblMember mem on enr.MemberID = mem.MemberID
join tblTrOOPbalance tr on mem.MANumber = tr.MedicareNumber
where enr.EnrollmentID in ('16823','16828')
答案 1 :(得分:0)
我相信这update
更好。 tblEnrollment是否有主键或唯一列?
update tblEnrollment
set TrOOPCurrentYrBalanceAmt = (
select tr.TrOOPCurrentYrBalanceAmt
from tblTrOOPbalance tr
where tr.MemberID = tblEnrollment.MemberID
)
where EnrollmentID in ('16823', '16828')
这是一个可能对一次性修复有用的黑客攻击:
declare @done bit = 0;
while @done = 0 begin
update top(1) tblEnrollment
set TrOOPCurrentYrBalanceAmt = (
select tr.TrOOPCurrentYrBalanceAmt
from tblTrOOPbalance tr
where tr.MemberID = tblEnrollment.MemberID
)
where EnrollmentID in ('16823', '16828')
and coalesce(TrOOPCurrentYrBalanceAmt, -999999) <>
coalesce((
select tr.TrOOPCurrentYrBalanceAmt
from tblTrOOPbalance tr
where tr.MemberID = tblEnrollment.MemberID
), -999999);
set @done = case when @@rowcount = 0 then 1 else 0 end
end
您可以使用以下方法进行检查和调试:
select
TrOOPCurrentYrBalanceAmt as TrOOPCurrentYrBalanceAmt1,
(
select tr.TrOOPCurrentYrBalanceAmt
from tblTrOOPbalance tr
where tr.MemberID = tblEnrollment.MemberID
) as TrOOPCurrentYrBalanceAmt2
from tblEnrollment
where EnrollmentID in ('16823', '16828')
and coalesce(TrOOPCurrentYrBalanceAmt, -999999) <>
coalesce((
select tr.TrOOPCurrentYrBalanceAmt
from tblTrOOPbalance tr
where tr.MemberID = tblEnrollment.MemberID
), -999999);