Generate Triggers for Newly created tables

  • Hi All,

    I have many new tables for which i need to write Insert,Update and delete triggers manually. Is there any way to generate triggers script which takes table name as a input parameter and print/generate trigger's script?

    Thanks in Advance.

    Regards,

    Vijay

  • vijay.singh 14112 (9/11/2014)


    Hi All,

    I have many new tables for which i need to write Insert,Update and delete triggers manually. Is there any way to generate triggers script which takes table name as a input parameter and print/generate trigger's script?

    Thanks in Advance.

    Regards,

    Vijay

    Yes... just write the generic dynamic SQL to create a table, get the column names from the sys.columns or INFORMATION_SCHEMA.columns, and you're done. I did it for the creation of audit triggers.

    If someone makes the suggestion of using a generic CLR or a generic T-SQL trigger that can simply be copied from one table to another, be well advised that they pretty much suck for performance because they have to materialize the INSERTED and DELETED tables to make the trigger work because those tables are out of scope for anything dynamic. It's best to do what you're doing... have code to write hard-coded triggers.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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