user1835591 user1835591 - 1 year ago 47
SQL Question

SQL Server trigger when table is changed

In my database, I have a specific request.

For some tables, let's call one

, I have a copy of it, let's call it
that needs to save some changes made on the records of the first one.

So, whenever I add a column to
, it needs to be added to the second one.

Since I'm doing it in a lot of tables, I cant simply duplicate the add column script.

I was thinking on using a DDL script for it, but cant seem to find a working solution.

Basically, what I need is to have a trigger, that whenever a table schema is changed, it would replicate the said change to the "copy" table.

Thanks in advance.

Answer Source

What you are looking for is a DDL ("data definition language") trigger. You can specify such a trigger on alter table -- to capture when a new column is added.

The place to start is the documentation.