我从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字段中将powersyclaimsauth中的每一行更新为0。
任何人都可以帮我解决我做错的事吗?
谢谢!
答案 0 :(得分:4)
第二个连接条件错误。试试这个:
AFTER DELETE
AS
begin
update a set isUsed = 0
from dbo.warrantyclaimsauth a
inner join deleted d on a.warrantyAuthNo = d.warrantyAuthNo
end
您加入的deleted
warrantyauthno
等于自己,而不是deleted
表中的值。您基本上只是cross join
deleted
而不是过滤任何行。
答案 1 :(得分:3)
inner join deleted d on a.warrantyAuthNo = a.warrantyAuthNo
应该是
inner join deleted d on a.warrantyAuthNo = d.warrantyAuthNo
您目前有一个简单的连接条件,它提供的交叉连接不会过滤到deleted
答案 2 :(得分:3)
....
AFTER DELETE
AS
begin
update dbo.warrantyclaimsauth set isUsed = 0
from dbo.warrantyclaimsauth a
inner join deleted d on a.warrantyAuthNo = d.warrantyAuthNo
end