我有两张桌子tbl_PurchaseDetails
和tbl_ItemDetails
. tbl_ItemDetails
我需要在from中插入一些行tbl_PurchaseDetails
,就在它插入之后tbl_PurchaseDetails
。我知道这个问题,但我无法解决它。请帮忙。
我为触发器编写了以下代码:
CREATE TRIGGER trigger_UpdateItemDetails ON tbl_PurchaseDetails
FOR INSERT AS
DECLARE @PurchaseID VARCHAR(20)
DECLARE @Quantity INT
DECLARE @WarehouseID VARCHAR(20)
SELECT @PurchaseID=(PurchaseID) FROM INSERTED
SELECT @Quantity=(ItemQuantity) FROM INSERTED
SELECT @WarehouseID=(WarehouseID) FROM INSERTED
INSERT INTO
tbl_ItemDetails
(PurchaseID,Quantity,WarehouseID)
VALUES
(
@PurchaseID,@Quantity,@WarehouseID
)
现在,当我插入tbl_PurchaseDetails
行时,添加到tbl_PurchaseDetails
但不添加到tbl_ItemDetails
. 它抛出以下错误:
消息 515,级别 16,状态 2,过程 trigger_UpdateItemDetails,第 11 行
无法将值 NULL 插入列“PurchaseID”,表“dbStockHandling.dbo.tbl_ItemDetails”;列不允许空值。插入失败。
我的问题是如何获取插入的值,tbl_PurchaseDetails
以便触发器可以将它们插入tbl_ItemDetails
?