SHARE
Facebook X Pinterest WhatsApp

Using DMO to Enable/Disable Triggers

Written By
thumbnail
Andy Warren
Andy Warren
Jan 15, 2001

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

 

Recommended for you...

Best Online Courses to Learn SQL
Ronnie Payne
Sep 23, 2022
Best Courses for Database Administrators
Ronnie Payne
Jul 22, 2022
Tip 74 – Changing Cost Threshold for Parallelism
Gregory Larsen
Feb 24, 2021
How Many Databases Can You Name?
Brad Jones
May 11, 2020
Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.