How to get the trigger table ?

  • Hi,

    can somebody told me how to access the related

    table from inside a trigger ?

    I want to use the trigger table without coding

    the table name as a constant - any idea ?

    Thanks in advance.

  • Check BOL "Using the inserted and deleted Tables"

  • Hey,

    thanks for the tip... but...

    I don't want to use the inserted and deleted table-I

    want to get the name of the base table so that

    I can use it in a dynamic sql string.

  • Look up sysobjects in BOL. 

     

    The parent_obj  column contains the parent table of triggers.

  • that's true, but I still need the name of the trigger as a

    constant, isn't it ? The point is, I don't want to use a constant in the trigger code, so that the trigger himself can get the own table name.

  • Ummm, your original post said you didn't want to have to use the TABLE name as a constant. 

    I can't imagine why using the trigger name inside the trigger would be a problem, but if it is, I don't know of any other way for a trigger to reference itself, so you may be stuck with it.

  • Using the trigger name inside of the trigger is not the problem. I have more than 100 tables, and only one trigger code (the trigger will be defined on each table, all triggers has the same code). Inside the trigger, the name of the trigger table will be used to query some data, so that I want to get them without coding the table name or the trigger name in the trigger body. Doing so, I will be able to generate the create script for the triggers by querying the system tables.

    Anyway, best thanks for Your answer.

  • Has anybody got any further ideas on this? its something I am trying to do as well, but with little success.

  • Sussed it..

    DECLARE @TableName varchar(256)

    SELECT @TableName=s2.[name]

    FROM sysobjects s

    INNER JOIN sysobjects s2 ON s2.id=s.parent_obj

    WHERE s.id=@@PROCID AND s.type='tr' AND s.name='[[name of trigger]]'

  • See if this helps:

    http://www.umachandar.com/technical/SQL6x70Scripts/Main47.htm

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Thanks, great link - had helped.

Viewing 11 posts - 1 through 10 (of 10 total)

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