Firing trigger for bulk insert

后端 未结 3 1393
一生所求
一生所求 2021-01-05 02:23
ALTER TRIGGER [dbo].[TR_O_SALESMAN_INS]
   ON  [dbo].[O_SALESMAN]
   AFTER INSERT
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- inte         


        
3条回答
  •  旧巷少年郎
    2021-01-05 02:37

    My scenerio is, (Table Names: Stock, StockLog)

    I am inserting bulk rows inside Stock table through a Stored Procedure and want to also have all these rows inside StockLog table

    first i was doing same as you (by variables) inside my insert trigger for Stock table but getting error because by using

    DECLARE @StocklId bigint
    
    SET @StocklId = (SELECT StocklId FROM inserted)
    

    i was having multiple values by (SELECT StocklId FROM inserted) as i was inserting multiple rows, then i remove all variables, and now i am doing this

    INSERT INTO StockLog(StocklId,PharmacyId,TransactionDetailId,ProductId,TotalQty,ReservedQty,AvailableQty,strUserName,strTerminalName,strVer)
    
    SELECT StocklId, PharmacyId, TransactionDetailId, ProductId, TotalQty, 0, AvailableQty,strUserName, strTerminalName, strVer FROM inserted
    

    and now everything is fine

提交回复
热议问题