Showing posts with label triggers. Show all posts
Showing posts with label triggers. Show all posts

Wednesday, May 8, 2013

Script Trigger in SQL Server 2008

Here is the query to script out all Triggers in SQL Server 2008:


set nocount on

DECLARE name nvarchar(128)

DECLARE procCursor CURSOR
FOR
SELECT distinct Name FROM sysobjects o
Inner join syscomments c on OBJECTPROPERTY(o.id, N'IsTrigger') = 1 and c.id= o.id

OPEN procCursor
FETCH
NEXT FROM procCursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN

PRINT 'if exists (select * from dbo.sysobjects '
PRINT ' where id = object_id(N' + char(39) + '[dbo].[' + @Name + ']' + char(39) + ')'
PRINT ' and OBJECTPROPERTY(id, N' + char(39) + 'IsTrigger' + char(39) + ') = 1) '
PRINT '  DROP TRIGGER ' + @Name 
PRINT ' GO '
PRINT ' SET QUOTED_IDENTIFIER on '
PRINT ' GO '
PRINT ' SET ANSI_NULLS on '
PRINT ' GO'
exec sp_helptext @Name
PRINT ' GO '
FETCH NEXT FROM procCursor INTO @name
END
CLOSE procCursor
DEALLOCATE procCursor
GO