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
Solved

SQL Server Trigger

Posted on 2003-12-08
6
1,117 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: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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

828 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