【发布时间】:2012-01-10 06:38:47
【问题描述】:
我从 SqlServer 2000 中的以下触发器开始
ALTER TRIGGER DeleteTrigger
ON [dbo].[WarrantyClaimsLineItems]
FOR DELETE
AS
begin
declare @wa int
SET @wa = (SELECT warrantyAuthNo from DELETED)
update dbo.warrantyclaimsauth
set isused = 0
WHERE warrantyClaimsAuth.warrantyauthno = @wa
end
基本上,每当从表中删除一个行项目时,我都需要将分配给该行项目的warrantyauth.isUsed 更新回false。
删除一个订单项时有效,但删除多个订单项时出现以下错误。
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
我明白为什么,触发器只希望删除一条记录。 所以我需要对其进行修改以处理多行删除。
我做了一些研究并将我的触发器修改为这个
....
AFTER DELETE
AS
begin
update dbo.warrantyclaimsauth set isUsed = 0
from dbo.warrantyclaimsauth a
inner join deleted d on a.warrantyAuthNo = a.warrantyAuthNo
end
但是,这会在 isUsed 字段中将warrantyclaimsauth 中的每一行更新为0。 谁能帮我解决我做错的事情? 谢谢!
【问题讨论】: