下面是我的查询。
CREATE TRIGGER MyTrigger ON CUSTOMERS
AFTER INSERT
AS
if exists ( select * from CUSTOMERS t
inner join inserted i on i.name<>t.name or i.age<>t.AGE or i.address<>t.address or i.salary<>t.SALARY
and i.id = t.id)
begin
UPDATE c SET c.name = i.name, c.age = i.age c.address=i.address FROM CUSTOMERS as c INNER JOIN inserted i ON c.id = i.id
end go
在此更新语句中,我想更新ID匹配的所有列和行,但是当我尝试插入时,在ID匹配的地方更新了旧行并插入了新记录
答案 0 :(得分:0)
如果我是对的,您可以尝试使用内部联接进行更新,如下所示。
CREATE TRIGGER MyTrigger ON CUSTOMERS
AFTER INSERT
AS
if Exists (select * from CUSTOMERS t
inner join inserted i on i.id=t.id
and i.age=t.AGE
and i.[address]=t.[address]
and i.salary=t.SALARY
and i.id <> t.id)
begin
UPDATE c
SET c.name = i.name,
c.age = i.age
--...and so on
FROM CUSTOMERS as c
INNER JOIN inserted i ON c.id <> i.id
end