HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMajor

How can I drop all triggers in a single database?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canalldatabasedropsinglehowtriggers

Problem

I have a database with 104 triggers, is there a way to delete all the triggers with a single command from a single database called 'system_db_audits?

Solution

You can use Dynamic SQL and the sys.triggers DMV to build query that you can execute.

is_ms_shipped excludes any triggers that were shipped with SQL Server.

parent_class_desc filters for object level triggers, rather than database level.

Change the PRINT to an EXEC once you are happy with the output.

USE system_db_audits;
GO

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += 
    N'DROP TRIGGER ' + 
    QUOTENAME(OBJECT_SCHEMA_NAME(t.object_id)) + N'.' + 
    QUOTENAME(t.name) + N'; ' + NCHAR(13)
FROM sys.triggers AS t
WHERE t.is_ms_shipped = 0
  AND t.parent_class_desc = N'OBJECT_OR_COLUMN';

PRINT @sql;

Code Snippets

USE system_db_audits;
GO

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += 
    N'DROP TRIGGER ' + 
    QUOTENAME(OBJECT_SCHEMA_NAME(t.object_id)) + N'.' + 
    QUOTENAME(t.name) + N'; ' + NCHAR(13)
FROM sys.triggers AS t
WHERE t.is_ms_shipped = 0
  AND t.parent_class_desc = N'OBJECT_OR_COLUMN';

PRINT @sql;

Context

StackExchange Database Administrators Q#125617, answer score: 38

Revisions (0)

No revisions yet.