Identifying a trigger

  • Hey folks . . .

    I've set up a simple trigger, using the syntax as follows:

    create trigger [some trigger name]

    on [some table]

    after insert, update, delete

    as

    [sql code -- blah blah blah blah]

    Here's my question: is there for my sql code (the code that's executed by the trigger, that is) to determine what kind of action was executed (insert, update, or delete)? It executes the same code, regardless of what action it was, but I would like to keep track of what action executed it.

    Is there any way to do this without writing separate triggers, one for "after insert," one for "after update," etc.?

    "The wonderful thing about triggers is that triggers are wonderful things . . . " (sorry, had to! :hehe:)

    +--------------------------------------------------------------------------------------+
    Check out my blog at https://pianorayk.wordpress.com/

  • You can do something like:

    CREATE TRIGGER [TRIGGER-NAME] ON [TABLE-NAME] FOR INSERT, UPDATE, DELETE

    AS

    BEGIN

    SET NOCOUNT ON

    DECLARE @Action CHAR(1)

    SELECT @Action = CASE WHEN EXISTS(SELECT 1 FROM INSERTED) AND EXISTS(SELECT 1 FROM DELETED) THEN 'U' WHEN EXISTS(SELECT 1 FROM INSERTED) THEN 'I' ELSE 'D' END

    -- the rest of code ...

    END

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Yep, I just conferred with my boss (who is a DBA), and he gave me something very similar. I'll mess around with it and see what I can do with it.

    Thanks for the assist!

    +--------------------------------------------------------------------------------------+
    Check out my blog at https://pianorayk.wordpress.com/

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply