Triggering input into another table...

  • create table TZ(Z_id int identity(1,1) primary key, z_name varchar(20) not null)

    select * from TZ

    delete tz where Z_id in (3)

    insert into TZ values('r u')

    create table TY(Y_id int identity(100,1) primary key, y_name varchar(10) not null)

    select * from TY

    insert into TY values('2wards u')

    create trigger Ztrigger

    on TZ for

    insert as

    begin

    //declare @value varchar(10)

    //set @value = TZ.z_name

    //insert TY values(@value)

    insert TY values(' ')

    end

    insert tz values('glacier') -- Triggering the trigger through insert statement.


    The scenario is i have created two tables TZ&TY with identical fields, and creating a trigger(on TZ table) to insert values automatically to TY, but on Triggering the trigger by using an insert statement... I want glacier to be the value of Y_name in the table TY.  Can some1 let me know how do i do that...(I thought of creating a local variable but that idea failed.) 

    Regards

    Arun


    Kindest Regards,

    Arunkumar

    Reputation is what other people know about you. Honor is what you know about yourself."--

  • You use the virtual table INSERTED, which contains all the rows that where inserted by the statement that triggered the trigger.

    create trigger Ztrigger

    on TZ for

    insert as

    begin

      INSERT INTO TY (y_name)

      SELECT z_name FROM INSERTED

    end

  • Hit on the bull's eye, that is what is wanted...

    Chris, just started working on sql and when would i provide solution for others like this.

    3 Cheers

    Arun


    Kindest Regards,

    Arunkumar

    Reputation is what other people know about you. Honor is what you know about yourself."--

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

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