Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Add trigger to all table at once

$
0
0

I created trigger to one table like this 

CREATE TRIGGER REPPartition_WorkflowStatus 
ON [dbo].[WorkflowStatus]
AFTER INSERT, UPDATE 
AS 
	UPDATE [dbo].[WorkflowStatus]
	SET [PartitionId] = (SELECT [PartitionSettingId] FROM [dbo].[PartitionSetting] where [InstanceName] = @@SERVERNAME)
	WHERE [StatusID] IN(SELECT [StatusID] from inserted)
BEGIN
	SET NOCOUNT ON
END 

it works fine, but I have 100 table which have the same column id "

PartitionId

 and every Primary Key filed is table name plus the ID, so no problem with that naming convention

so I tried to do like this 

EXEC sp_MSforeachtable
       '
		CREATE TRIGGER REPPartition?
		ON [dbo].[?]
        AFTER INSERT, UPDATE 
		AS          
	    UPDATE [dbo].[Workflow]
	    SET [PartitionId] = (SELECT [PartitionSettingId] FROM [dbo].[PartitionSetting] where [InstanceName] = @@SERVERNAME)
	    WHERE [?ID] IN(SELECT [?ID] from inserted)
BEGIN
	SET NOCOUNT ON
END 
	'

but it doesnt work... anyone who can help me?

 

Binyam Welday


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>