我有2张桌子,发货和发票。我有一个after触发器,可以使用发票表中的发票号更新装运表。发票表具有其相关的装运号,每个发票可以多个。
Create Trigger ShipmentInvoice
on Therefore.dbo.Invoice
For Insert
AS
Update cat set
invoice = Fac.invoice
FROM therefore.dbo.thecat10 cat
INNER JOIN therefore.dbo.vFacturaAlbaran alb
on alb.shipment COLLATE Modern_Spanish_CI_AS = alb.shipment
这有效,但是每次都会更新整个表。我需要一种方法,仅在激活触发器时才使用最近在发票表中添加的值来更新装运表。
触发器在发票表上方。我正在使用发票表中的发票号更新发货表。
答案 0 :(得分:0)
这有效,但是每次都会更新整个表。我需要一种方法,仅在激活触发器时才使用最近在发票表中添加的值来更新装运表。
因为要同时连接两个表,所以需要使用INSERTED
表来仅获取插入的行,如:
Create Trigger ShipmentInvoice
on Therefore.dbo.Invoice
For Insert
AS
Update cat set
invoice = I.invoice
FROM therefore.dbo.thecat10 cat
INNER JOIN INSERTED I alb ON Cat.ID = I.ID
...
答案 1 :(得分:0)
将表更新为已经插入的状态似乎很奇怪;您为什么不事先解决正确的值?这是伪SQL,但是会让您走上正确的路
{
"cID" : "w2-502952",
"soldToId" : "34124",
}
...
...
请注意括号(CREATE TRIGGER ShipmentInvoice
ON Therefore.dbo.Invoice
FOR INSERT
AS
UPDATE cat
SET invoice = Fac.invoice --There is no table/alias Fac, where is this coming from? inserted?
FROM therefore.dbo.thecat10 cat
JOIN inserted i ON cat.{Primary/Foreign ID COLUMN} = i.{Primary/Foreign ID COLUMN}
INNER JOIN therefore.dbo.vFacturaAlbaran alb ON alb.shipment COLLATE Modern_Spanish_CI_AS = alb.shipment;
)中的项目以及我对对象{}
的评论。