SQL Script to Set Triggers not for replication

Posted on 2005-05-12
Last Modified: 2012-08-13
I am setting up Transactional replication, I need to set all the triggers in the production database to Not for Replication. At the moment I am using the Alter Trigger command to add the not for replication option for each trigger. But there is like 250 Triggers , is there a faster way or any sp i can use to make my life easiler now and in the future ?

many Thanks

Question by:yiping
    LVL 4

    Assisted Solution

    select the triggers to file from query anal
    like below

    SELECT  '/*****   ''   *****/'+Char(13)+
          syscomments.text +Char(13)+'Zx~@#zxZ'+char(13)
          char(13)+'GO'+CHAR(13)AS [Trigger Definition]
          sysobjects INNER JOIN syscomments ON =
          (sysobjects.xtype = 'TR')

    then in my favorite editor (textpad)
    replace the "NOT FOR REPLICATION" with nothing
    and then replace all of the ''Zx~@#zxZ'' with FOR REPLICATION

    use something that you wont have in your triggers
    i generally use some ascii codes above 128 to search and relace
    but thats up to you

    save the fixed script and run it (preferably on a test db first)

    LVL 20

    Accepted Solution

    This works better for me than other solutions.  Additionally, with this code you're free to do other things to the trigger code if you want.

    ' ExportTriggers.vbs
    ' VBScript to export all trigger scripts from a SQL Server database
    ' Written By: Lynn Ransdell
    ' invoke like this:  cscript ExportTriggers.vbs > AllTriggers.sql

    Dim server   :  server = "server\instance"
    Dim dbname   :  dbname = "testdb"
    Dim user     :  user   = "sa"
    Dim pswd     :  pswd   = "secret"
    Dim conn, rs, rs2
    Dim dblcrlf
    Dim Line

       dblcrlf = chr(13) & chr(10) & chr(13) & chr(10)

       ConnStr = "provider=SQLOLEDB;network=DBMSSOCN;"
       ConnStr = ConnStr & "uid=" & user & ";pwd=" & pswd & ";server="
       ConnStr = ConnStr & server & ";database=" & dbname

       Set Conn = CreateObject("ADODB.Connection")
       Conn.Open ConnStr

       Set rs = Conn.Execute("SELECT name FROM sysobjects WHERE xtype='TR'")
       Do While Not rs.eof
          Wscript.Echo "/*****  " & rs(0) & "  *****/"
          Set rs2 = Conn.Execute("EXEC sp_helptext '" & rs(0) & "'")
          Do While Not rs2.eof

             ' It seems that each line of the trigger text has double CRLF characters...
             ' These 2 statements remove both pairs and the "wscript.echo" adds 1 back on
             Line = Rtrim(rs2(0))
             Line = Left(Line,Len(Line)-2)
             Wscript.Echo Line

       rs.Close   : Set rs   = nothing
       Conn.Close : Set Conn = nothing


    Author Comment

    Thanks so much made my life much easiler

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Creating and Managing Databases with phpMyAdmin in cPanel.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now