Add automatically a record in a linked table

  • Hi

    Well, i've two tables <Table1> and <Table2> with a jonction table between them.

    Table1 : Id1, Col1_Table1, Col2_Table1 ...

    Table2 : Id2, Col1_Table2, Col2_Table2...

    Jonction table : Id1, Id2

    I would like to add automatically by the means of the relation between the tables in SQLServer, a recording in Table2 when i insert one in Table1.

    How can i do this in SQLServer, without creating a additional request ?

    Thx

  • you need to create a trigger on table 1

    something along the lines if

    create trigger t1 on table1 for INSERT,UPDATE as

    if @rowcount=1

    begin

    if (select count from table2 where x=(select y from inserted))=0 then

    begin

    insert into table2 .......

    end

    end

    or something along those lines - see books online "create trigger" for more details

    MVDBA

  • Ok. thx a lot.

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

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