getting the table name in a trigger

  • Hi All

    im doing some exploring and testing on triggers here

    im trying to build a dynamic trigger that i can copy past on any table

    my current problem is - how can i get the table name the trigger was fired on ????

  • I'm not sure if I understand what you're trying to accomplish.  The parent table of a trigger is explicitly identified in the trigger create statement:

    CREATE TRIGGER tr_name

    on MyTable

    for insert, ....

    Simply the act of creating the trigger provides the table name.

    There is a non-standard method using system tables that will work.  This has all the usual disclaimers about the use of system tables, no guarantees about migration to future versions, etc.

    In the body of your trigger, the following will provide the parent table:

    DECLARE @parent_table

    SELECT @parent_table = object_name(parent_obj)

    FROM    sysobjects

    WHERE  id = @@procid

    As I said above, though, this really is unnecessary.

     

    Scott Thornburg

  • thx 🙂

    and i need it because i want the changes from 1 trigger to another to be as minimal as possible

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

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