混淆触发器:我有两种类型的记录,'L'库和'N'正常。当更新或插入'N'时,我需要更新相应的'L'。 'L'记录的更新不应该自行更新。 此代码不会让更新成功。为什么?Microsoft SQL 2005简单触发器如何编写不更新任何内容的触发器?
ALTER trigger updateProductLibrary
on Product
after update as
BEGIN
-- either deleted (old) or inserted (new)
declare @counter int, @insertedType char(1)
set @insertedType = 'Z'
select @insertedType = i.type
from inserted i
if(@insertedType = 'N')
begin
select @counter = count(*)
from product p join inserted i on
p.sku = i.sku and
p.type = 'L' -- for library
if(@counter > 0) -- update
BEGIN
update p set name = i.name
from product p join inserted i on
p.sku = i.sku and
p.type = 'L'
END
ELSE -- insert
BEGIN
insert into product (sku, name, type)
select i.sku, i.name, 'L'
from inserted i
END
END
END
哇。我完全错过了。谢谢 – 2010-11-20 20:50:18