Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Ignore Column in Trigger Based Update Audit

Author  Topic 

deadtrees
Starting Member

26 Posts

Posted - 2014-09-30 : 16:32:58
I'm trying to use the Pop Rivett generic trigger based auditing.

http://www.nigelrivett.net/AuditTrailTrigger.html

I want to ignore an SSMA_TimeStamp column, but I don't know how.

I've commented out the lines that I tried in the code I pasted below, and I am able to get the SSMA_TimeStamp field to not be logged by the audit, but when I do, the audit double enters the field I updated. The column name is SSMA_TimeStamp and the Ordinal Number of that field on the table is 18 if that's necessary for anything.

Thanks!

ALTER trigger [dbo].[tr_impressions] on [dbo].[Impressions] for insert, update, delete
as

declare @bit int ,
@field int ,
@maxfield int ,
@char int ,
@fieldname varchar(128) ,
@TableName varchar(128) ,
@PKCols varchar(1000) ,
@sql varchar(2000),
@UpdateDate varchar(21) ,
@UserName varchar(128) ,
@Type char(1) ,
@PKSelect varchar(1000)

select @TableName = 'Impressions'

-- date and user
select @UserName = system_user ,
@UpdateDate = convert(varchar(8), getdate(), 112) + ' ' + convert(varchar(12), getdate(), 114)

-- Action
if exists (select * from inserted)
if exists (select * from deleted)
select @Type = 'U'
else
select @Type = 'I'
else
select @Type = 'D'

-- get list of columns
select * into #ins from inserted
select * into #del from deleted

-- Get primary key columns for full outer join
select @PKCols = coalesce(@PKCols + ' and', ' on') + ' i.' + c.COLUMN_NAME + ' = d.' + c.COLUMN_NAME
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
where pk.TABLE_NAME = @TableName
and CONSTRAINT_TYPE = 'PRIMARY KEY'
and c.TABLE_NAME = pk.TABLE_NAME
and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME

-- Get primary key select for insert
select @PKSelect = coalesce(@PKSelect+'+','') + '+convert(varchar(100),coalesce(i.' + COLUMN_NAME +',d.' + COLUMN_NAME + '))'
--select @PKSelect = coalesce(@PKSelect+'+','') + '''<' + COLUMN_NAME + '=''+convert(varchar(100),coalesce(i.' + COLUMN_NAME +',d.' + COLUMN_NAME + '))+''>'''
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
where pk.TABLE_NAME = @TableName
and CONSTRAINT_TYPE = 'PRIMARY KEY'
and c.TABLE_NAME = pk.TABLE_NAME
and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME

if @PKCols is null
begin
raiserror('no PK on table %s', 16, -1, @TableName)
return
end

--select @field = 0, @maxfield = max(ORDINAL_POSITION) from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @TableName
IF @Type = 'I' or @Type = 'D'
BEGIN
SET @maxfield = 1
SET @field = 0
END
ELSE
BEGIN
SELECT @field = 0,

@maxfield = MAX(ORDINAL_POSITION)

FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName
End
while @field < @maxfield
begin
select @field = min(ORDINAL_POSITION) from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @TableName and ORDINAL_POSITION > @field
select @bit = (@field - 1 )% 8 + 1
select @bit = power(2,@bit - 1)
select @char = ((@field - 1) / 8) + 1
if substring(COLUMNS_UPDATED(),@char, 1) & @bit > 0 or @Type in ('I','D')
begin
select @fieldname = COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @TableName and ORDINAL_POSITION = @field -- AND COLUMN_NAME <> 'SSMA_TimeStamp'
select @sql = 'insert Audit (Type, TableName, PK, FieldName, OldValue, NewValue, UpdateDate, UserName)'
select @sql = @sql + ' select Distinct''' + @Type + ''''
select @sql = @sql + ',''' + @TableName + ''''
select @sql = @sql + ',' + @PKSelect
select @sql = @sql + ',''' + @fieldname + ''''
select @sql = @sql + ',convert(varchar(1000),d.' + '['+ @fieldname + ']' + ')'
select @sql = @sql + ',convert(varchar(1000),i.' + '[' + @fieldname + ']' + ')'
select @sql = @sql + ',''' + @UpdateDate + ''''
select @sql = @sql + ',''' + @UserName + ''''
select @sql = @sql + ' from #ins i full outer join #del d'
select @sql = @sql + @PKCols
select @sql = @sql + ' where i.' + '[' + @fieldname + ']' + ' <> d.' + '[' + @fieldname + ']'
select @sql = @sql + ' or (i.' + '[' + @fieldname + ']' + ' is null and d.' + '[' + @fieldname + ']' + ' is not null)'
select @sql = @sql + ' or (i.' + '[' + @fieldname +']' + ' is not null and d.' + '[' + @fieldname + ']' + ' is null)'
--select @sql = @sql + ' and ['+ COLUMN_NAME + '] not like ' + 'SSMA_TimeStamp'
--select @sql = @sql + ' and (i.' + '[' + @fieldname +']' + ' not like ' + 'SSMA_TimeStamp' + ' or d.' + '[' + @fieldname + ']' + ' not like ' + 'SSMA_TimeStamp' + ')'
exec (@sql)
end
end
   

- Advertisement -