Lo script seguente può essere utilizzato per disabilitare tutti i trigger nel database di SQL Server.
USE [Database]
GO
DECLARE @TriggerName AS VARCHAR(500)
DECLARE @TableName AS VARCHAR(500)
DECLARE @SchemaName AS VARCHAR(100)
--Disabilita tutti i trigger in un database in SQL Server
DECLARE DisableTrigger CURSOR FOR
SELECT TBL.name AS TableName,
Schema_name(TBL.schema_id) AS Table_SchemaName,
TRG .name AS TriggerName
DA sys.triggers TRG
INNER JOIN sys.tables TBL
SU TBL.OBJECT_ID =TRG.parent_id
AND TRG.is_disabled=0
AND TBL.is_ms_shipped=0
OPEN DisableTrigger
RECUPERA Avanti DA DisableTrigger IN @TableName,@SchemaName,@TriggerName
WHILE @@FETCH_STATUS =0
BEGIN
DECLARE @SQL VARCHAR(MAX)=NU LL
SET @SQL='Disable Trigger ' + @TriggerName +' ON '+@SchemaName+'.'+@TableName
EXEC (@SQL)
PRINT 'Trigger ::' + @TriggerName + 'è disabilitato su '+@SchemaName+'.'+@TableName
PRINT @SQL
FETCH Next FROM DisableTrigger INTO @TableName,@SchemaName, @TriggerName
END
CLOSE DisableTrigger
DEALLOCATE DisableTrigger
Per verificare se tutti i trigger sono disabilitati correttamente in SQL Database del server, utilizzare la query seguente
SELECT TBL.name AS TableName,
Schema_name(TBL.schema_id) AS Table_SchemaName,
TRG.name AS TriggerName,
TRG. parent_class_desc,
CASE
QUANDO TRG.is_disabled =0 THEN 'Enable'
ELSE 'Disable'
END AS TRG_Status
DA sys.triggers TRG
INNER UNISCITI a sys.tables TBL
SU TBL.OBJECT_ID =TRG.parent_id
AND trg.is_disabled=1 --usa questo filtro per ottenere Trigger disabilitati