Solved

SQL Server Trigger how do I know what columns are updated when I do have 150 columns in a table

Posted on 2006-10-19
6
2,494 Views
Last Modified: 2008-01-09
I have a sql server table that has 150 columns. I wrote a trigger that sends an email to me telling me about some fields from this table. New Information  added or already existing info updates. I only deal with around 15 columns here. I wanted to know, in case of update, what all columns are actually updated. I don't want to write each and every column to see if it is updated. Columns_Updated i don't know the logic behind it. Please suggest. Thanks.
0
Comment
Question by:wilkersons
6 Comments
 
LVL 20

Accepted Solution

by:
Sirees earned 168 total points
ID: 17768793
FROM BOL

COLUMNS_UPDATED())

Tests, in an INSERT or UPDATE trigger only, whether the mentioned column or columns were inserted or updated. COLUMNS_UPDATED returns a varbinary bit pattern that indicates which columns in the table were inserted or updated.

The COLUMNS_UPDATED function returns the bits in order from left to right, with the least significant bit being the leftmost. The leftmost bit represents the first column in the table; the next bit to the right represents the second column, and so on. COLUMNS_UPDATED returns multiple bytes if the table on which the trigger is created contains more than 8 columns, with the least significant byte being the leftmost. COLUMNS_UPDATED will return the TRUE value for all columns in INSERT actions because the columns have either explicit values or implicit (NULL) values inserted.

COLUMNS_UPDATED can be used anywhere inside the body of the trigger.

0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 166 total points
ID: 17769120
>> I don't want to write each and every column to see if it is updated. <<

That's the best way to do it, unless you will dynamically determine the column names from system tables/views at run time.  Otherwise, COLUMNS_UPDATED() is too tricky to use properly and maintain.
0
 

Expert Comment

by:andieje
ID: 17769929
Hello Scott

I'm shining the batman light for help.

Would you be able to help me on this post. There is a word document that contains my current design.

I would appreciate it very much.
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_22023997.html

wilkersons - sorry to piggyback your post
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 166 total points
ID: 17771124
And you should understand that IF UPDATE() and COLUMNS_UPDATED() do not tell you if the data actually changed.  All they tell you was if the column(s) articipated in an UPDATE statement.  In order to check to see if the data actually changed you would have to check the value of the column in the Deleted logical table and compare it to the corresponding column in the Inserted logical table.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

920 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

14 Experts available now in Live!

Get 1:1 Help Now