完全联接-两个具有相同属性和主键但值不同的表当我执行完全连接时,输出是什么?

tjvv9vkg  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(232)

我正在编写触发器函数,并希望注意数据上发生的修改。用于更新、删除和插入。我正在尝试使用触发器中插入和删除的表对它们进行完全联接。但我不知道 result.OrderID 是主键。

select @orderID = isnull (i.OrderID, d.OrderID)
from inserted i
full join deleted d on i.OrderID = d.OrderID;
qojgxg4l

qojgxg4l1#

我想我会用 exists :

update SaleOrder
    set LastModified = @Lastmodified
    where exists (select 1 from inserted i where i.OrderId = SaleOrder.OrderId) or
          exists (select 1 from deleted d where d.OrderId = SaleOrder.OrderId) ;
xxls0lw8

xxls0lw82#

如果我理解正确的话,这会更好:

UPDATE so
SET LastModified = @LastModified
FROM SaleOrder so
INNER JOIN (SELECT OrderId 
           FROM Inserted 
           UNION
           SELECT OrderId
           FROM Deleted) as t
  ON so.OrderId = t.OrderId

或者另一种方式:

UPDATE SaleOrder
SET LastModified = @LastModified
WHERE OrderId IN (SELECT OrderId 
                  FROM Inserted 
                  UNION
                  SELECT OrderId
                  FROM Deleted)

相关问题