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 2005 Forums
 Transact-SQL (2005)
 Update Trigger question

Author  Topic 

dhw
Constraint Violating Yak Guru

332 Posts

Posted - 2009-10-01 : 14:50:41
Hi All -

I have a table that has 49 columns (it is part of a legacy system I recently inherited!).

I have a new task to create an audit record of any change to the table. The requirements will NOT allow me to simply create a snapshot of the original row (using the deleted table in the trigger) into an audit table. Instead, they are requiring that the audit process capture the column changed and the old value for ANY column change.

The audit table will have a structure like:
AuditID (primary key, identity column)
TableName (varchar (30) ) - the table that was changed
ColumnName (varhcar (30)) - the column affected
ColumnValue (varchar (100) - the old value
AuditDate (current date...via getdate())

Because there are so many columns, I don't see any other way to do this other than using the Columns_Updated() function.

My current plan is to figure out how to determine the column that was changed and then write each change to the above mentioned table.

I had been considering a series of IF UPDATED(column1), IF UPDATED(column2), as that was easier to read.

I think that either way this will be a bit of overhead for every update.

Just wondering if anyone has other thoughts or suggestions.

thanks
- will

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-10-01 : 14:59:38
as told , you can use colums_updated to check which all columns were updated and use catalog view information_schema.columns to get column names
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-01 : 15:00:05
to add insult to injury:
I would do 49 IF-statements and would not believe to updated() because the update could happen with the same value...
I would compare inserted vs. deleted for every column.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

dhw
Constraint Violating Yak Guru

332 Posts

Posted - 2009-10-01 : 15:53:28
quote:
Originally posted by visakh16

as told , you can use colums_updated to check which all columns were updated and use catalog view information_schema.columns to get column names



How would I be able to use the information_schema.columns view to get the column names? Is there a way to get that by using the Columns_Updated() bitmask result?

thanks
- will
Go to Top of Page

dhw
Constraint Violating Yak Guru

332 Posts

Posted - 2009-10-01 : 15:55:45
quote:
Originally posted by webfred

to add insult to injury:
I would do 49 IF-statements and would not believe to updated() because the update could happen with the same value...
I would compare inserted vs. deleted for every column.



That was my intial thought, well, using the 49 IF-statements. But when I found the Columns_Updated() function, I thought that might be a more "elegant" way to handle this situation. Maybe not?

Regarding the comparison, that is a great idea. I'll have to check, but I do think that the application itself does this check before it even attempts an Update to the database. I'll confirm and if not, your suggestion will be used.

Thanks
- will
Go to Top of Page

dhw
Constraint Violating Yak Guru

332 Posts

Posted - 2009-10-01 : 17:21:09
Hi all -

I found a good example of using Columns_Updated() and it works well in identifying which (or all) of the 49 columns were updated within the Update Trigger.

I am struggling to figure out how to JOIN to the Deleted and Inserted tables to get the Old and New values.

Here is the current trigger code.


ALTER trigger [dbo].[tr_trigtest]
on [dbo].[patient]
for update
as
declare @bit int ,
@field int ,
@char int,
@oldVal varchar(100),
@newVal varchar(100)

select @field = 0
while @field < (select max(colid)
from syscolumns
where id = (select id from sysobjects where name = 'patient'))
begin
select @field = @field + 1
select @bit = (@field - 1 )% 8 + 1
select @bit = power(2,@bit - 1)
select @char = ((@field - 1) / 8) + 1
--select @char, @field, @bit -- debug code to check the bits that are tested.
if substring(COLUMNS_UPDATED(),@char, 1) & @bit > 0
select @field, name as ColumnName from syscolumns where colid = @field
and id = (select id from sysobjects where name = 'patient')

end



thanks for any help or suggestions.
Go to Top of Page
   

- Advertisement -