home

-- Create a trigger to log updates to any field
Author Nigel Rivett
This code creates a table with 50 fields
Adds a single row.
Generates the code to create a trigger on the table to log all updates to fields and the new values.
This generator can easily be put in a stored procedure with the table name as parameter.
If the SP is then run on a selection of tables it will generate a script to create the triggers.


-- create the test table
drop table trigtest
go
create table trigtest
(
i01 int
)
declare @i int, @s varchar(1000)
select @i = 1
while @i < 50
begin
	select @i = @i + 1
	select @s = 'alter table trigtest add i' + right('00' + convert(varchar(2),@i),2) + ' int'
	exec (@s)
end
go

declare @i int, @s varchar(1000)
select @i = 0
while @i < 50
begin
	select @i = @i + 1
	select @s = coalesce(@s + ',1', 'insert trigtest select 1')
end
exec (@s)
go

-- Create the trigger script
declare @table varchar(128)
declare @col varchar(128)
select @table = 'trigtest'

set nocount on
declare @s varchar(1000)

print 'drop trigger tr_' + @table
print 'go'  
print 'create trigger tr_' + @table + ' on ' + @table + ' for update'  
print 'as'

declare @i int, @j int
select @j = -1
while @j < (select max(colid)-1 from syscolumns where id = (select id from sysobjects where name = @table))
begin
select @j = @j + 1
select @col = name from syscolumns where colid = @j + 1 and id = (select id from sysobjects where name = @table)
select @s = 'if update(' + @col + ')'
print @s
select @s = 'select ''' + @col + ''' , ' + @col + ' from inserted'
print @s 
end
print 'go'
print ''

-- code generated
drop trigger tr_trigtest
go
create trigger tr_trigtest on trigtest for update
as
if update(i01)
select 'i01' , i01 from inserted
if update(i02)
select 'i02' , i02 from inserted
if update(i03)
select 'i03' , i03 from inserted
if update(i04)
select 'i04' , i04 from inserted
if update(i05)
select 'i05' , i05 from inserted
if update(i06)
select 'i06' , i06 from inserted
if update(i07)
select 'i07' , i07 from inserted
if update(i08)
select 'i08' , i08 from inserted
if update(i09)
select 'i09' , i09 from inserted
if update(i10)
select 'i10' , i10 from inserted
if update(i11)
select 'i11' , i11 from inserted
if update(i12)
select 'i12' , i12 from inserted
if update(i13)
select 'i13' , i13 from inserted
if update(i14)
select 'i14' , i14 from inserted
if update(i15)
select 'i15' , i15 from inserted
if update(i16)
select 'i16' , i16 from inserted
if update(i17)
select 'i17' , i17 from inserted
if update(i18)
select 'i18' , i18 from inserted
if update(i19)
select 'i19' , i19 from inserted
if update(i20)
select 'i20' , i20 from inserted
if update(i21)
select 'i21' , i21 from inserted
if update(i22)
select 'i22' , i22 from inserted
if update(i23)
select 'i23' , i23 from inserted
if update(i24)
select 'i24' , i24 from inserted
if update(i25)
select 'i25' , i25 from inserted
if update(i26)
select 'i26' , i26 from inserted
if update(i27)
select 'i27' , i27 from inserted
if update(i28)
select 'i28' , i28 from inserted
if update(i29)
select 'i29' , i29 from inserted
if update(i30)
select 'i30' , i30 from inserted
if update(i31)
select 'i31' , i31 from inserted
if update(i32)
select 'i32' , i32 from inserted
if update(i33)
select 'i33' , i33 from inserted
if update(i34)
select 'i34' , i34 from inserted
if update(i35)
select 'i35' , i35 from inserted
if update(i36)
select 'i36' , i36 from inserted
if update(i37)
select 'i37' , i37 from inserted
if update(i38)
select 'i38' , i38 from inserted
if update(i39)
select 'i39' , i39 from inserted
if update(i40)
select 'i40' , i40 from inserted
if update(i41)
select 'i41' , i41 from inserted
if update(i42)
select 'i42' , i42 from inserted
if update(i43)
select 'i43' , i43 from inserted
if update(i44)
select 'i44' , i44 from inserted
if update(i45)
select 'i45' , i45 from inserted
if update(i46)
select 'i46' , i46 from inserted
if update(i47)
select 'i47' , i47 from inserted
if update(i48)
select 'i48' , i48 from inserted
if update(i49)
select 'i49' , i49 from inserted
if update(i50)
select 'i50' , i50 from inserted
go
 

home