Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Using DMO to Enable and Disable Triggers

By Andy Warren, (first published: 2001/05/18)

These code samples illustrate how to enable and disable all triggers in a database at one time - a task not easily done otherwise! If you haven't use DMO before, I've posted two articles that you may find informative - Introduction to SQL-DMO and More DMO

 

If you wanted to do this using plain vanilla SQL you would need to create a script that had one "Alter Table xx Disable Trigger yy" statement per trigger. As written these should run in VBScript or as an ActiveX job. If you would like to use them in VB you should set a reference to the SQL-DMO object library so that you get statement completion and can do strong data typing. One advantage to using code for a task like this is you can easily modify it to only run on tables that start with 'A' or on triggers that contact the word 'Insert' in their name.

 

Note (2/26/01): Brian Knight has posted an alternative that you may like better if you prefer TSQL to code! I've also just posted a new DMO article demonstrating how to do a restore

 

Enable Trigger

Dim oServer
Dim oTable
Dim oTrigger
dim DBName

'change this to your database name
DBName="PUBS" 

Set oServer = createobject("SQLDMO.SQLServer")

oServer.LoginSecure = True
oServer.Connect

For Each oTable In oServer.Databases(DBName).Tables
    For Each oTrigger In oTable.Triggers
        oTrigger.enabled=true
    Next
Next

oServer.DisConnect
Set oServer = Nothing

Disable Trigger

Dim oServer
Dim oTable
Dim oTrigger
dim DBName

'change this to your database name
DBName="PUBS" 

Set oServer = createobject("SQLDMO.SQLServer")

oServer.LoginSecure = True
oServer.Connect

For Each oTable In oServer.Databases(DBName).Tables
    For Each oTrigger In oTable.Triggers
        oTrigger.enabled=false
    Next
Next

oServer.DisConnect
Set oServer = Nothing

 

Total article views: 7636 | Views in the last 30 days: 4
 
Related Articles
FORUM

query(dbname,size,recoverymodel)

query(dbname,size,recoverymodel)

FORUM

query(dbname,size,recoverymodel)

query(dbname,size,recoverymodel)

FORUM

Trigger

Trigger

FORUM

Triggers

Trigger

FORUM

Triggers

Trigger

Tags
programming    
sql-dmo    
 
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