Audit Trigger Problems...please help

  • Dear Sir,

    I have been racking my mine on how to get this trigger below to work. I have some tables in my database which have no Primary keys but foreign keys in instead, because they are look up tables, the problem i am getting is the the trigger will not fire. Any ideas on how i can look up those tables without inserting a primary key? The other question i have is that in one of these tables i have an image in for a digital signature, but i get this error saying it can not handle text,ntext or image? Is there someway to suppress anyone of those fields in the trigger so it would skip that column?

    create trigger tr_tblcustomers on tblcustomers 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)

    select @TableName = 'tblcustomers'

    -- date and user

    select @UserName = system_user ,

    @UpdateDate = convert(varchar(8), getdate(), 112) + ' ' + convert(varchar(12), getdate(), 114)

    -- 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

    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

    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

    begin

    select @fieldname = COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @TableName and ORDINAL_POSITION = @field

    select @sql = 'insert Audit (TableName, FieldName, OldValue, NewValue, UpdateDate, UserName)'

    select @sql = @sql + ' select ''' + @TableName + ''''

    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)'

    exec (@sql)

    end

    end

    Moe C


    Moe C

  • quote:


    The other question i have is that in one of these tables i have an image in for a digital signature, but i get this error saying it can not handle text,ntext or image? Is there someway to suppress anyone of those fields in the trigger so it would skip that column?


    Will it help if you expand the one statement to

    select @field = min(ORDINAL_POSITION) 
    
    From INFORMATION_SCHEMA.COLUMNS
    Where TABLE_NAME = @TableName
    AND ORDINAL_POSITION > @field
    AND (Not (DATA_TYPE in ('Text','NTEXT')))

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply