Solved

SQL Server Trigger

Posted on 2003-12-08
6
1,116 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always 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: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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

919 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

12 Experts available now in Live!

Get 1:1 Help Now