SQL Server - after insert trigger - update another column in the same table

后端 未结 7 831
一向
一向 2020-12-04 22:27

I\'ve got this database trigger:

CREATE TRIGGER setDescToUpper
ON part_numbers
 AFTER INSERT,UPDATE
AS
DECLARE @PnumPkid int, @PDesc nvarchar(128)

SET @Pnum         


        
7条回答
  •  Happy的楠姐
    2020-12-04 23:09

    It depends on the recursion level for triggers currently set on the DB.

    If you do this:

    SP_CONFIGURE 'nested_triggers',0
    GO
    RECONFIGURE
    GO
    

    Or this:

    ALTER DATABASE db_name
    SET RECURSIVE_TRIGGERS OFF
    

    That trigger above won't be called again, and you would be safe (unless you get into some kind of deadlock; that could be possible but maybe I'm wrong).

    Still, I do not think this is a good idea. A better option would be using an INSTEAD OF trigger. That way you would avoid executing the first (manual) update over the DB. Only the one defined inside the trigger would be executed.

    An INSTEAD OF INSERT trigger would be like this:

    CREATE TRIGGER setDescToUpper ON part_numbers
    INSTEAD OF INSERT
    AS
    BEGIN
        INSERT INTO part_numbers (
            colA,
            colB,
            part_description
        ) SELECT
            colA,
            colB,
            UPPER(part_description)
        ) FROM
            INSERTED
    END
    GO
    

    This would automagically "replace" the original INSERT statement by this one, with an explicit UPPER call applied to the part_description field.

    An INSTEAD OF UPDATE trigger would be similar (and I don't advise you to create a single trigger, keep them separated).

    Also, this addresses @Martin comment: it works for multirow inserts/updates (your example does not).

提交回复
热议问题