Firing trigger for bulk insert

后端 未结 3 1294
一生所求
一生所求 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:29

    you can use a cursor as follows:

    create trigger trg_insertstuff
    on [O_SALESMAN]
    after insert
    as
    
      DECLARE @SLSMAN_CD NVARCHAR(20)
        DECLARE @SLSMAN_NAME NVARCHAR(20)
    
    
     declare db_cursor CURSOR FOR  
    SELECT SLSMAN_CD, SLSMAN_NAME
    from inserted
    
    OPEN db_cursor  
    FETCH NEXT FROM db_cursor INTO @SLSMAN_CD , @SLSMAN_NAME 
    
    WHILE @@FETCH_STATUS = 0  
    BEGIN  
    
    IF NOT EXISTS(SELECT * FROM O_SALESMAN_USER WHERE SLSMAN_CD = @SLSMAN_CD)
        BEGIN
            INSERT INTO O_SALESMAN_USER(SLSMAN_CD, PASSWORD, USER_CD)
            VALUES(@SLSMAN_CD, @SLSMAN_CD,@SLSMAN_NAME )
        END
    
    
    FETCH NEXT FROM db_cursor INTO  @SLSMAN_CD , @SLSMAN_NAME 
    end
    
    CLOSE db_cursor  
    DEALLOCATE db_cursor
    
    0 讨论(0)
  • 2021-01-05 02:36

    Yes, triggers fire once per statement, not once per row.

    So you need something like:

    SET NOCOUNT ON
    
    INSERT INTO O_SALESMAN_USER(SLSMAN_CD, PASSWORD, USER_CD)
    SELECT
        i.SLSMAN_CD,i.SLSMAN_CD,i.SLSMAN_NAME
    FROM
        inserted i
            left join
        O_SALESMAN_USER u
            on
                i.SLSMAN_CD = u.SLSMAN_CD
    where
        u.SLSMAN_CD is NULL
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题