SQL Server “AFTER INSERT” trigger doesn't see the just-inserted row

后端 未结 12 1988
庸人自扰
庸人自扰 2020-12-23 14:05

Consider this trigger:

ALTER TRIGGER myTrigger 
   ON someTable 
   AFTER INSERT
AS BEGIN
  DELETE FROM someTable
         WHERE ISNUMERIC(someField) = 1
END         


        
12条回答
  •  盖世英雄少女心
    2020-12-23 14:18

    MS-SQL has a setting to prevent recursive trigger firing. This is confirgured via the sp_configure stored proceedure, where you can turn recursive or nested triggers on or off.

    In this case, it would be possible, if you turn off recursive triggers to link the record from the inserted table via the primary key, and make changes to the record.

    In the specific case in the question, it is not really a problem, because the result is to delete the record, which won't refire this particular trigger, but in general that could be a valid approach. We implemented optimistic concurrency this way.

    The code for your trigger that could be used in this way would be:

    ALTER TRIGGER myTrigger
        ON someTable
        AFTER INSERT
    AS BEGIN
    DELETE FROM someTable
        INNER JOIN inserted on inserted.primarykey = someTable.primarykey
        WHERE ISNUMERIC(inserted.someField) = 1
    END
    

提交回复
热议问题