Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Jan 14, 2001

Using DMO to Enable/Disable Triggers

By Andy Warren

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

 



MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date