SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Copy Triggers from One DB to Another

By Dwain Camps,

The other day I had to use the SQL scripts wizard (in SSMS right click the database name, select Tasks and then Generate Scripts) to create a copy of the database.  I selected all of the objects, went throught the motions and then created my new database without issue.

The wizard is a pretty good tool, but I came to realize that either I missed something or the wizard did.  My newly created database didn't contain any of the triggers in the old one, and there were a bunch.

In my case, the source database was stored on a remote SQL server, so I needed to write a script that would copy all of the triggers over to the new database which I was temporarily storing locally.

Hence I came up with the script here.  Before you run it, you should do a little checking.

Run the following:


FROM sys.servers

to see if the source server is listed.  If it is, you do not need to run the two EXECs:

EXEC sp_addlinkedserver @server='SOURCESQLSERVER'


EXEC sp_dropserver @server='SOURCESQLSERVER'

When the script runs, it prints each table for which it processes (CREATEs) a trigger in the messages pane.  This is so that it is easy to identify when there's an error in the CREATE step (if the names of your triggers aren't consistent or easily identifiable by which table they apply to).  This was beneficial to me because I was upgrading the database compatibility level from 80 to 100 and there were a few triggers that failed to CREATE due to some minor syntax incompatibilities.

Hope this is useful to you.

Total article views: 709 | Views in the last 30 days: 1
Related Articles

create trigger

create a trigger on SQL Server that will cause an email to be sent to my alias when a database is cr...


How to Script all Triggers from DB into Text File?

Scripting Triggers and Creating on Other DB


Server level trigger at Database level...

Database level trigger at server level


Script Generates Audit Triggers

This script generates a audit trigger for each table in the database. The trigger then logs when eac...


CREATE TRIGGER with ado.net and C#

Can't create trigger