Disable Enable Trigger SQL server for a table

Sql ServerTriggers

Sql Server Problem Overview


I want to create one proc like below but it has error on syntax. Could anyone pointing out the problem?

Create PROCEDURE [dbo].[my_proc] AS

BEGIN

DISABLE TRIGGER dbo.tr_name ON dbo.table_name

-- some update statement

ENABLE TRIGGER dbo.tr_name  ON dbo.table_name

END

** Error Message : Incorrect syntax near 'ENABLE'.

Sql Server Solutions


Solution 1 - Sql Server

use the following commands instead:

ALTER TABLE table_name DISABLE TRIGGER tr_name

ALTER TABLE table_name ENABLE TRIGGER tr_name

Solution 2 - Sql Server

The line before needs to end with a ; because in SQL DISABLE is not a keyword. For example:

BEGIN
;
DISABLE TRIGGER ...

Solution 3 - Sql Server

As Mark mentioned, the previous statement should be ended in semi-colon. So you can use:

; DISABLE TRIGGER dbo.tr_name ON dbo.table_name

Solution 4 - Sql Server

After the ENABLE TRIGGER OR DISABLE TRIGGER in a new line write GO, Example:

DISABLE TRIGGER dbo.tr_name ON dbo.table_name

GO
-- some update statement

ENABLE TRIGGER dbo.tr_name  ON dbo.table_name

GO

Solution 5 - Sql Server

I wanted to share something that helped me out. Idea credit goes to @Siavash and @Shahab Naseer.

I needed something where I could script disable and re enable of triggers for a particular table. I normally try and stay away from tiggers, but sometimes they could be good to use.

I took the script above and added a join to the sysobjects so I could filter by table name. This script will disable a trigger or triggers for a table.

select 'alter table '+ (select Schema_name(schema_id) from sys.objects o 
where o.object_id = parent_id) + '.'+object_name(parent_id) + ' ENABLE TRIGGER '+ t.Name as EnableScript,*
from sys.triggers t 
INNER JOIN dbo.sysobjects DS ON DS.id = t.parent_id 
where is_disabled = 0 AND DS.name = 'tblSubContact'

Solution 6 - Sql Server

Below is the Dynamic Script to enable or disable the Triggers.

select 'alter table '+ (select Schema_name(schema_id) from sys.objects o 
where o.object_id = parent_id) + '.'+object_name(parent_id) + ' ENABLE TRIGGER '+
Name as EnableScript,*
from sys.triggers t 
where is_disabled = 1

Solution 7 - Sql Server

if you want to execute ENABLE TRIGGER Directly From Source :

we can't write like this:

Conn.Execute "ENABLE TRIGGER trigger_name ON table_name"

instead, we can write :

Conn.Execute "ALTER TABLE table_name DISABLE TRIGGER trigger_name"

Solution 8 - Sql Server

USE [DatabaseName]
GO

-- HABILITAR TRIGGERS
SELECT 'ALTER TABLE ['+ 
	( SELECT SCHEMA_NAME(SCHEMA_ID) FROM [sys].[objects] AS O WHERE O.[object_id] = T.[parent_id]) 
		+ '].[' + OBJECT_NAME(T.[parent_id]) + '] ENABLE TRIGGER '+ T.[name] + ';' AS [EnableScript], *
		FROM [sys].[triggers] AS T 
			INNER JOIN [sys].[sysobjects] DS ON DS.[id] = T.[parent_id]
		WHERE T.[is_disabled] = 0 
			--AND DS.[name] = 'TableName'

-- DESHABILITAR TRIGGERS
SELECT 'ALTER TABLE ['+ 
	( SELECT SCHEMA_NAME(SCHEMA_ID) FROM [sys].[objects] AS O WHERE O.[object_id] = T.[parent_id]) 
		+ '].[' + OBJECT_NAME(T.[parent_id]) + '] DISABLE TRIGGER '+ T.[name] + ';' AS [EnableScript], *
		FROM [sys].[triggers] AS T 
			INNER JOIN [sys].[sysobjects] DS ON DS.[id] = T.[parent_id]
		WHERE T.[is_disabled] = 0 
			--AND DS.[name] = 'TableName'

Solution 9 - Sql Server

Below is the simplest way

Try the code

ALTER TRIGGER trigger_name DISABLE

That's it :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpangView Question on Stackoverflow
Solution 1 - Sql ServerWael DalloulView Answer on Stackoverflow
Solution 2 - Sql ServerMark KaneView Answer on Stackoverflow
Solution 3 - Sql ServerkaptanView Answer on Stackoverflow
Solution 4 - Sql ServerAndreaView Answer on Stackoverflow
Solution 5 - Sql ServerJay WalkerView Answer on Stackoverflow
Solution 6 - Sql ServerShahab NaseerView Answer on Stackoverflow
Solution 7 - Sql Serverar3003View Answer on Stackoverflow
Solution 8 - Sql ServerFrancisco J. NaranjoView Answer on Stackoverflow
Solution 9 - Sql Serverkavita londheView Answer on Stackoverflow