Home Forums Programming General Can I return an identity value to be used in an insert trigger on a table? RE: Can I return an identity value to be used in an insert trigger on a table?

  • njfuller (2/6/2013)


    In this case, the applications group is inserting only one tb1 record in a transaction.

    1 or more tb2 records could be inserted.

    I would like the identity value from the tb1 insert returned so I can use it to update on all the tb2 records.

    (It would be nice if the appl would do this for me, but not in their scope) :).

    i know you abstracted everything out (tbl1/tbl2) to simplify, but it doesn't look right.

    if i insert into tbl1, you KNOW there is already a matching row in tbl2? or should that be an insert into tbl2 when tbl1 gets inserted?

    create TRIGGER [dbo].[trg_tb1_Insert]

    ON [dbo].[tb_tb1]

    AFTER INSERT

    AS

    BEGIN

    SET NOCOUNT ON;

    UPDATE dbo.tb2

    SET tb2_id = Inserted.ColumnWithIdentity

    FROM Inserted

    WHERE Inserted.tb1_id = dbo.tb2.tb1_id

    END

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!