0

I need to insert a row into a table if a particular row in another table is updated.

How do I do an IF statement in the the DB trigger on Table1, saying if Table1.column1 = 'TC' then INSERT a row in Table2.

1 Answer 1

1

You would do this in an update trigger on the 'other' table.

There are two special tables in triggers: inserted and deleted. You join these two tables in such a way that the result set is the rows you wish to insert. Ergo -

create trigger [after_update_on_Table1] on [Table1] for update
as
    ...
    insert
        into [Table2] (...)
    select
        ...
    from
        inserted as i
            inner join
        deleted as d
            on (i.<*pk*> = d.<*pk*>)
    where
        <*other conditions if applicable*>
    ...

<pk> is whatever the appropriate primary key would be. If this is a compound primary key then AND together the different primary key components.

For what you describe thus far you do not require an if statement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.