Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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,504 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
[X]
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
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

721 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