basic of trigger

  • hi all

    good morning,

    i want trigger to insert data to table2 once data insert in table1.

    i have nearly 150 tables with different number of columns.

    please guide me how to make one common insert query for all tables in trigger!!!!

    please if u have any basic trigger material plz give to me, it help me alot.

    thanks for all!!!!

    --------------------------------------------------------

    I am Learner -- SQL

  • CREATE TRIGGER AfterInserttb1

    ON tb1

    AFTER INSERT

    AS

    BEGIN

    SET NOCOUNT ON;

    --if columns do not match than

    --use variable to insert the data

    INSERT INTO tb2

    SELECT * FROM inserted

    --columns dont match

    DECLARE @Col1 VARCHAR(10)

    DECLARE @Col2 VARCHAR(10)

    DECLARE @Col3 VARCHAR(10)

    SELECT @Col1=Col1,

    @Col2=Col2,

    @Col3=Col3

    FROM inserted

    INSERT INTO @tb2(Col1,Col2,Col3)

    SELECT @Col1,@Col2,@Col3

    END

    GO

    this is a simple example

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

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