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,493 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

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)

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

760 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

19 Experts available now in Live!

Get 1:1 Help Now