I am new to SQL Server 2008 and I need advice from all of you. I want to find out the changed value in inserted
and deleted
tables of the SQL Server 2008 for a table because I am currently doing the audit trail to keep the old and new value. How can I loop all the column to find out which field's value change from the deleted and inserted table? I had tried the if else statement to compare
For example:
create trigger trg_XXX on dbo.table
after update
as
begin
declare
@oldID varchar(6),
@newID varchar(6)
select @oldID = ID from deleted
select @newID = ID from inserted
if(@oldID != @newID)
insert into table (a, b) values (@oldID, @newID)
else
print('do nothing')
Is there a way to do using cursor to loop through the deleted and inserted table or any alternative way? Can give me some example?
I think you are looking for what unofficially called
magic tables
in SQL server.The tables "
INSERTED
" and "DELETED
" are called magic tables of the SQL Server. We can not see these tables in the data base. But we can access these tables from the "TRIGGER
"When we insert the record into the table, the magic table "
INSERTED
" will be created In that table the current inserted row will be available. We can access this record in the "TRIGGER
".When we update a record on the table where trigger is created, the magic tables "
INSERTED
" and "DELETED
" both will be created, the Old data of the updating record will be available in "DELETED" table and, the new data will be available in "INSERTED" table, while accessing them inside the trigger.When we delete the record from the table, the magic table "
DELETED
" will be created In that table the current deleted row will be available. We can access this record in the "TRIGGER
".Example:
Following code Explains the magic table "
INSERTED
":Following code Explain the magic table "
DELETED
"Sources(All credit to these articles & authors):
http://www.codeproject.com/Questions/285423/what-is-magic-table-different-types-of-magic-table http://www.dotnetspider.com/resources/29332-Magic-tables-SQL-Server.aspx
. .
Alternatively
, you can try: Obtaining Changes by Using the Change Tracking Functions, its MSDN link to explain how to track changes by using in-built functions.
CHANGETABLE(CHANGES …)
functionThis rowset function is used to query for change information. The function queries the data stored in the internal change tracking tables. The function returns a results set that contains the primary keys of rows that have changed together with other change information such as the operation, columns updated and version for the row.
CHANGE_TRACKING_CURRENT_VERSION()
functionIs used to obtain the current version that will be used the next time when querying changes. This version represents the version of the last committed transaction.
CHANGE_TRACKING_MIN_VALID_VERSION()
functionIs used to obtain the minimum valid version that a client can have and still obtain valid results from CHANGETABLE(). The client should check the last synchronization version against the value thatis returned by this function. If the last synchronization version is less than the version returned by this function, the client will be unable to obtain valid results from CHANGETABLE() and will have to reinitialize.
Refer syntax & usage at http://technet.microsoft.com/en-us/library/cc280358%28v=sql.105%29.aspx
Hope it helps.
You could make a dynamic "comparer" by converting the inserted and deleted tables into 2 xml, iterating through the properties and comparing the xml value.
Example:
First: do NOT use a cursor inside a trigger - EVER!
Second: to identify what fields were included in the update statement you can use: UPDATE() or COLUMNS_UPDATED()
Note: This does NOT list the fields that have had their value changed, just the list of columns included in the SET portion of the UPDATE statement.
Third: There are a variety of methods that you can use to audit changes to your tables (the accepted answer on Most efficient method to detect column change in MS SQL Server has a good list and guidelines on how to use, if you are using SQL Server Enterprise you could also look at using Change Data Capture
Some example audit code I would use (where I want to record a column by column audit table):
It would be simpler (from a sql perspective and faster [due to less writes] to audit by row) ie:
I'm not quite sure, what your goal ist, i think it might be something like this. Let's say we have a table like this:
and some audit table like this:
Then you create a trigger:
There you have it.