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,499 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:Scott Pletcher
Scott Pletcher 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS On fail action 5 38
SQL Backup Question 2 30
MS SQL Server select from Sub Table 14 26
Requesting help with creating an SQL query to select similar records 4 15
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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

827 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