Solved

SQL Server Trigger

Posted on 2003-12-08
6
1,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
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:
Scott Pletcher 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

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:Scott Pletcher
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

729 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