Solved

SQL Server Trigger

Posted on 2003-12-08
6
1,115 Views
Last Modified: 2008-02-01
Hi experts,
  I have a trigger for insert, update and delete of any record in a table. Sometimes, user clicks update button even if he does not change anything. In these cases, the trigger creates new row in the audit table. The number of rows increase rapidly even when nothing is changed. So I want to insert into audit table only when something is changed. Please inform me how I can do this. I have about 40 to 50 columns in my table.
Thanks so much.
0
Comment
Question by:code2002
  • 3
  • 2
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 9897615
Unfortunately, you will have to compare the columns yourself and see if any are different, something like:

INSERT INTO auditTable
SELECT ...
FROM inserted.i
INNER JOIN auditTable [at] ON i.key = [at].key
WHERE i.col1 <> [at].col1 OR i.col2 <> [at].col2 OR ...
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 9897633
To make this easier, you can use a quick script to generate the code, for example:

SELECT 'i.[' + name + '] <> ' + '[at].[' + name + ']' + ' OR '
FROM syscolumns
WHERE OBJECT_NAME(id) = 'table_name'
--AND name NOT IN ('key_name1', 'key_name1', ...)

then copy/paste, removing the last "OR".
0
 
LVL 4

Expert Comment

by:szacks
ID: 9898032
Use the Columns_Updated feature in the trigger.
While I've never tried it, I imagine that if all you want to check is if no values have changed you could just compare it to 0.
Another thought is in your application, don't enable the button until something has been modified. That would prevent the extra processing.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:code2002
ID: 9898253
ScottPletcher,
thanks for ur reply. but will the quick script handle the null values ? for example, if one of the values is null, then '<>' will not work for it. Please let me know what you think.
szacks,
I read in Sql Server books online that Columns_Updated works for first 8 columns. Moreover its complicated as number of columns increase (like my case).
Thanks.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 9898534
You make an excellent point about the nulls.  You would need to add a check for NULL, for example:

WHERE (ISNULL(i.[col1], 0) <> ISNULL([at].[col1], 0) OR ...

or:

WHERE ((i.[col1] IS NULL AND [at].[col1] IS NULL) OR i.[col1] <> [at].[col1]) OR ...


Btw, note that COLUMNS_UPDATE, or UPDATE(column), will be true if the column is referenced in the UPDATE, even if the value didn't change.  For example, if:

UPDATE tablex
SET colx = colx
WHERE ...

Then:

IF UPDATE(colx)

will be true, even though the value didn't change.
0
 

Author Comment

by:code2002
ID: 9904851
ScottPletcher,
sorry for the delay. Thanks for your help. I gave Grade A to you :) .
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now