Here is a simple script that will create the audit trigger for all user tables in your database. Obviously, if you want to not do it for all tables, modify the script accordingly... Thanks David for the example. In my case, the project Im using this on needs *every* change logged even if I don't know who the user is so this is useful to me.
-- Associate the generic CLR trigger with all user tables that aren't the audit
-- table and are not the mysterious dtProperties table which is labeled as a user
-- table as well...
declare @table varchar(128)
declare curTables cursor for
select name from sysobjects where xtype = 'U' and name != 'Audit' and category != 2
open curTables
fetch next from curTables into @table
while @@fetch_status = 0
begin
declare @sql varchar(1024)
declare @triggername varchar(128)
set @triggername = 'Audit_' + @table
if exists(select name from sysobjects where name = @triggername and xtype = 'TA')
begin
--drop the trigger
set @sql = 'drop trigger ' + @triggername
print @sql
execute(@sql)
end
set @sql = 'create trigger ' + @triggername + 'on ' + @table + ' for insert, update, delete as external name [AuditCommon].[AuditCommon.Triggers].AuditCommon'
print @sql
execute(@sql)
fetch next from curTables into @table
end
close curTables
deallocate curTables