Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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:

SELECT *

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: 670 | Views in the last 30 days: 5
 
Related Articles
FORUM

create trigger

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

FORUM

Server level trigger at Database level...

Database level trigger at server level

SCRIPT

Script Generates Audit Triggers

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

FORUM

CREATE TRIGGER with ado.net and C#

Can't create trigger

FORUM

How to Script all Triggers from DB into Text File?

Scripting Triggers and Creating on Other DB

Tags
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones