Can I dropped triggers and recreate them? Dropped is easy but I dont know how to recreate them. I need separate proc to drop and recreate triggers to avoid deadlock issues in package.

drop  triggers---
CREATE PROC [dbo].[SP_DropTriggers] 
AS
BEGIN
DECLARE @Table_Triggers SYSNAME, @trName sysname
DECLARE Cursor_Disabletriggers CURSOR FOR  
SELECT  object_name(parent_id) , name
FROM   sys.triggers  
OPEN Cursor_Disabletriggers
FETCH NEXT FROM   Cursor_Disabletriggers   
INTO @Table_Triggers  ,@trname
DECLARE @SQL nvarchar(max)
WHILE @@FETCH_STATUS = 0   
BEGIN  
SET @SQL  = 'drop trigger ' + 'dbo.'+@trname + ' on dbo.'+@Table_Triggers
select @sql
EXECUTE (@SQL)
FETCH NEXT FROM   Cursor_Disabletriggers INTO @Table_Triggers  ,@trname
END  
CLOSE Cursor_Disabletriggers
DEALLOCATE Cursor_Disabletriggers
END
I would like to recreate them in separate proc, How can I achieve this?

You need a permanent table to store the trigger code in:
create table
dbo.TriggerDefinitions
(TableName nvarchar(128) not null,
TriggerName nvarchar(128) not null,
TriggerDefinition nvarchar(max) not null,
constraint PK_TriggerDefinitions primary key (TableName, TriggerName))
go
In your procedure which drops the triggers you need to add code that stores the latest trigger definitions in the table above:
create procedure dbo.DropTriggers
as
begin
declare @TableName nvarchar(128),
@TriggerName nvarchar(128),
@TriggerDefinition nvarchar(max),
@DynamicSQL nvarchar(max)
declare TriggerCursor
cursor fast_forward for
select quotename(object_schema_name(po.object_id)) + '.' + quotename(po.name),
quotename(object_schema_name(co.object_id)) + '.' + quotename(co.name),
m.definition
from sys.sql_modules as m
join sys.objects as co
join sys.objects as po
on po.object_id = co.parent_object_id
on co.object_id = m.object_id
where co.type = 'tr'
open TriggerCursor
fetch next
from TriggerCursor
into @TableName,
@TriggerName,
@TriggerDefinition
while @@fetch_status = 0
begin
if exists(select top 1 1 from TriggerDefinitions where TableName = @TableName and TriggerDefinition = @TriggerDefinition)
update TriggerDefinitions
set TriggerDefinition = @TriggerDefinition
where TableName = @TableName
and TriggerName = @TriggerName
else
insert TriggerDefinitions
values (@TableName, @TriggerName, @TriggerDefinition)
if @@rowcount = 1
begin
set @DynamicSQL = 'drop trigger ' + @TriggerName + ' on ' + @TableName
print @DynamicSQL
--execute sp_executesql
-- @DynamicSQL
end
fetch next
from TriggerCursor
into @TableName,
@TriggerName,
@TriggerDefinition
end
close TriggerCursor
deallocate TriggerCursor
end
go
Finally the procedure which will re-create the triggers will simply query the table created and populated above and execute the code contained therein.
create procedure dbo.CreateTriggers
as
begin
declare @TableName nvarchar(128),
@TriggerName nvarchar(128),
@TriggerDefinition nvarchar(max),
@DynamicSQL nvarchar(max)
declare TriggerCursor
cursor fast_forward for
select *
from dbo.TriggerDefinitions
open TriggerCursor
fetch next
from TriggerCursor
into @TableName,
@TriggerName,
@TriggerDefinition
while @@fetch_status = 0
begin
set @DynamicSQL = @TriggerDefinition
print @DynamicSQL
--execute sp_executesql
-- @DynamicSQL
fetch next
from TriggerCursor
into @TableName,
@TriggerName,
@TriggerDefinition
end
close TriggerCursor
deallocate TriggerCursor
end
go
I've commented out the code which actually runs the dynamically created SQL.  You'll have to uncomment those lines for the code I've posted will work.

Similar Messages

Maybe you are looking for

  • Financial Reports Issue

    Hi, When I am trying to run Financial Reports in workspace getting the following erroer. *5200: Error executing query: Java.lang.ArithmeticException: / by zerojava.lang.ArithmeticExceptoion*. the issue with the only Planning reports. Consolidation re

  • JDK error in SolMan 7.0 installation

    I am installing SAP Solution Manager 7.0 on Windows 2003 Server R2 Enterprise x64 and Oracle 10.2.0.4 Patch 8 During the installation its asking me to install a JDK and enter the location. But when I give the path C:\j2sdk1.4.2_12 it says this JDK do

  • When deploying BPEL Process through JDeveloper java.security.AccessControlE

    1.) Navigated to the Connections tab of JDeveloper 10.1.3.2. 2.) Under the Application Server node, created a new Application Server connection, provided the relevant information on Type, Authentication & Connection's tab. 3.) On Authentication Tab,

  • Has Anyone Seen This Display Problem Before?

    This is a picture of the display from my sister's Power Mac G5. She installed 10.5.5 from the software update today. She said she was editing a document in Pages and all of a sudden it displayed this weird color pattern. http://www.pixelsandbits.net/

  • How can I make the newest version of iTunes for Windows run faster?

    I know I'm not the only one when I say that iTunes is by far one of the slowest programs on my computer.  I'm running Windows 7 and running the most recent version of iTunes for Windows.  Does anyone have some tips to make it run faster and smoother?