Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
2,508 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 672 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 664 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 664 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

926 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