How to Generate Scripts For All Triggers in Database Using Microsoft SQL Server Management Studio

前端 未结 5 954
小蘑菇
小蘑菇 2020-12-05 03:51

I\'d like to generate an SQL Script that contains the SQL to create all of the triggers that exist in our database. The triggers were added directly via the SSMS query pane

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 04:24

    I know the answer has been accepted already, but want to provide another solution for cases when for some reason SSMS wizard is not able to generate script for triggers (in my case it was MSSQL2008R2)

    This solution is based on idea from dana above, but uses 'sql_modules' instead to provide the full code of the trigger if it exceeds 4000 chars (restriction of 'text' column of 'syscomments' view)

    select [definition],'GO' from sys.sql_modules m
    inner join sys.objects obj on obj.object_id=m.object_id 
     where obj.type ='TR'
    

    Right click on the results grid and then "Save results as..." saves to file with formatting preserved

提交回复
热议问题