[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Single trigger on multiple tables

S.Scarciglia

3/20/2007 9:05:00 AM

Hi all,
I have a complex trigger that must be applied on almost every table of
my database. I'd like to create a SP or a FUNCTION with just one
parameter (the table name) and call it on every table in order to create
the trigger on the provided table.

Is it possible ? If not, have you an idea to solve my problem ?

Many thanks in advance !
Regards,
Salvatore
2 Answers

xyb

3/20/2007 9:39:00 AM

0

On 3?20?, ??5?05?, "S.Scarciglia" <salvatore.scarcig...@opis.it>
wrote:
> Hi all,
> I have a complex trigger that must be applied on almost every table of
> my database. I'd like to create a SP or a FUNCTION with just one
> parameter (the table name) and call it on every table in order to create
> the trigger on the provided table.
>
> Is it possible ? If not, have you an idea to solve my problem ?
>
> Many thanks in advance !
> Regards,
> Salvatore

use dynamic sql like below:
CREATE PROC s_BindTriggerToTable
@tableName VARCHAR(200)
AS

DECLARE @sqlString NVARCAHR(4000)
SELECT @sqlString = ''
SELECT @sqlString = @sqlString + 'CREATE TRIGGER ON TABLE ' +
@tableName + '
.....
--PRINT @sqlString
EXEC sp_executesql @sqlString
go

S.Scarciglia

3/20/2007 9:50:00 AM

0

> use dynamic sql like below:
> CREATE PROC s_BindTriggerToTable
> @tableName VARCHAR(200)
> AS
>
> DECLARE @sqlString NVARCAHR(4000)
> SELECT @sqlString = ''
> SELECT @sqlString = @sqlString + 'CREATE TRIGGER ON TABLE ' +
> @tableName + '
> ....
> --PRINT @sqlString
> EXEC sp_executesql @sqlString
> go
>

I think this is a solution, but I really don't like it for some reasons:
1. My trigger code is really long (90 lines of T-SQL)
2. My trigger already uses the execution of T-SQL inside a string
3. Hard code maintenance ! :-(

I hope I will find a better solution. If not, I will use your suggestions...

Thanks !
Salvatore