Согласно моему комментарию, я разработал этот SP для целей аудита, и он может изначально не работать для вас, но если вы измените его, я уверен, что он вам поможет.
В противном случае, измените вашвопрос о том, что вам нужно сделать триггеры, и я потом изменю свой ответ, чтобы работать на вас ...
if exists(select 1 from sysobjects where name = 'kv_sp_dbAudits_tr_AW')
drop procedure kv_sp_dbAudits_tr_AW
go
set ansi_nulls on
go
set quoted_identifier on
go
create procedure kv_sp_dbAudits_tr_AW(@tablename varchar(150),@switch bit)
as
BEGIN
if exists(select name from sysobjects where name = 'TempTableTriggers')
drop table TempTableTriggers;
create table TempTableTriggers (tablename varchar(100),columns varchar(max),columnvars varchar(max),columnvarst varchar(max),columnalloc varchar(max))
declare @query varchar(max)
, @loop int
, @dbname varchar(50) = (select db_name())+'_Audit'
, @table varchar(100)
, @tableau varchar(100)
, @trigger varchar(100)
, @column varchar(max)
, @columnvars varchar(max)
, @columnvarst varchar(max)
, @columnalloc varchar(max)
, @crlf char(2) = char(13)+char(10)
declare @Tables table
( id int identity primary key
, TableName varchar(100)
, TriggerName varchar(100)
, Columns varchar(max))
if @tablename like ''+'%'+''
set @tablename = @tablename
else
set @tablename = ''+@tablename+''
set @trigger = @tablename+'_Audit_kvtr_AW'
begin
if @switch = 1
begin
if exists(select name from sysobjects where name = @trigger)
begin
set @tablename = (select name from sysobjects where name = @tablename)
set @query = @crlf
set @query = @query + @crlf
set @query = @query + @crlf
set @query = @query + @crlf
set @query = @query + replicate('-',80)+@crlf
set @query = @query + @crlf
set @query = @query + 'The audit trigger "'+@tablename+'_Audit_kvtr_AW'+'" already exists!'
print (@query)
end
else
begin
if @tablename = 'all'
insert into @Tables
( TableName )
select
name
from sysobjects
where xtype = 'u'
and name not like '%audit%'
and name <> 'TempTableTriggers'
order by name
else
insert into @Tables
( TableName )
select
name
from sysobjects
where xtype = 'u'
and name not like '%audit%'
and name <> 'TempTableTriggers'
and name = @tablename
order by name
select @loop = min(id) from @Tables
while @loop is not null
begin
begin
set @query = ' declare @columns varchar(max)
select @columns = stuff((select '','' + char(10)+quotename(Column_Name)
from information_schema.columns
where table_name = '''+(select TableName from @Tables where id = @loop)+'''
and Column_Name <> ''cAllocs''
and data_type not in (''text'',''image'',''ntext'')
group by column_name, data_type, character_maximum_length, ordinal_position
order by ordinal_position
for xml path(''''), type).value(''.'', ''nvarchar(max)''),1,1,'''')
insert into TempTableTriggers (tablename,columns)
select distinct
table_name
, @columns
from information_schema.columns
where table_name = '''+(select TableName from @Tables where id = @loop)+''''
exec (@query)
end
select @loop = min(id) from @Tables where id>@loop
end
insert into @Tables
(TriggerName,TableName,Columns)
select
TableName,TableName,columns
from TempTableTriggers
select @loop = min(id) from @Tables
while @loop is not null
begin
begin
select @trigger = TriggerName+'_Audit_kvtr_AW'
, @tableau = TableName+'_Audit'
, @table = TableName
, @column = Columns
from @Tables
where id = @loop
set @query = 'create trigger '+@trigger+' on '+@table+@crlf
set @query = @query+ 'with encryption'+@crlf
set @query = @query+ 'after insert, update, delete'+@crlf
set @query = @query+ 'as'+@crlf
set @query = @query+ '/***********************************************************************************************************************************'+@crlf
set @query = @query+ 'Description : To insert any change made in '+@table+' into '+@dbname+'.dbo.'+@table+'_Audit'+@crlf
set @query = @query+ 'Author : Attie Wagner'+@crlf
set @query = @query+ 'Creation Date : 30 October 2018'+@crlf
set @query = @query+ 'Modified By : Attie Wagner'+@crlf
set @query = @query+ 'Modified Date : 28 January 2019'+@crlf
set @query = @query+ '************************************************************************************************************************************/'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'begin'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'set nocount on'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'if (select trigger_nestlevel(object_id('''+@trigger+'''))) > 1'+@crlf
set @query = @query+ 'return'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'declare @inserted varchar(15) = '''''+@crlf
set @query = @query+ 'declare @deleted varchar(15) = '''''+@crlf
set @query = @query+ 'declare @updated varchar(15) = '''''+@crlf
set @query = @query+ 'declare @action varchar(15) = '''''+@crlf
set @query = @query+ 'if((exists(select * from inserted)) and (exists(select * from deleted)))'+@crlf
set @query = @query+ ' set @updated = ''updated'''+@crlf
set @query = @query+ 'else'+@crlf
set @query = @query+ 'if(exists(select * from inserted))'+@crlf
set @query = @query+ ' set @inserted = ''new'''+@crlf
set @query = @query+ 'else'+@crlf
set @query = @query+ 'if(exists(select * from deleted))'+@crlf
set @query = @query+ ' set @deleted = ''deleted'''+@crlf
set @query = @query+ 'set @action = (select case when @inserted = ''new'' then ''new'' when @updated = ''updated'' then ''updated'' when @deleted = ''deleted'' then ''deleted'' else '''' end)'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'declare @kvAgent varchar(100) = (select cAgentName from _rtblAgents where idAgents = [dbo]._efnAgentIDFromConnection(''dbo''))'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'if @action in (''new'',''updated'')'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'begin'+@crlf
set @query = @query+ 'insert into ['+@dbname+'].dbo.'+@tableau+' ('+@column+','+@crlf+'kvUsername,'+@crlf+'kvDTStamp,'+@crlf+'kvAction)'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'select '+@column+','+@crlf+'@kvAgent,'+@crlf+'getdate(),'+@crlf+'@action'+@crlf
set @query = @query+ 'from inserted'+@crlf
set @query = @query+ 'end;'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'if @action = ''deleted'''+@crlf
set @query = @query+ @crlf
set @query = @query+ 'begin'+@crlf
set @query = @query+ 'insert into ['+@dbname+'].dbo.'+@tableau+' ('+@column+','+@crlf+'kvUsername,'+@crlf+'kvDTStamp,'+@crlf+'kvAction)'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'select '+@column+','+@crlf+'@kvAgent,'+@crlf+'getdate(),'+@crlf+'@action'+@crlf
set @query = @query+ 'from deleted'+@crlf
set @query = @query+ 'end;'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'begin'+@crlf
set @query = @query+ 'delete '+quotename(@dbname)+'.dbo.'+@table+'_Audit'+@crlf
set @query = @query+ 'where datediff(month,kvDTStamp,getdate()) > 12'+@crlf
set @query = @query+ 'end;'+@crlf
set @query = @query+ @crlf
set @query = @query+ 'end;'+@crlf
exec (@query)
end
select @loop = min(id) from @Tables where id>@loop
end
end
drop table TempTableTriggers;
end
else
begin
if @tablename = 'all'
insert into @Tables
( TriggerName )
select name from sysobjects
where xtype = 'tr'
and name like '%_Audit_kvtr_AW'
order by name
else
insert into @Tables
( TriggerName )
select name from sysobjects
where xtype = 'tr'
and name like '%_Audit_kvtr_AW'
and name = @tablename+'_Audit_kvtr_AW'
order by name
select @loop = min(id) from @Tables
while @loop is not null
begin
begin
set @trigger = (select TriggerName from @Tables where id = @loop)
set @query = 'if exists(select name from sysobjects where name = '''+@trigger+''')'+@crlf
set @query = @query+'drop trigger '+@trigger
exec (@query)
end
select @loop = min(id) from @Tables where id>@loop
end
end
end
END;