Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 567187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:03:24+00:00 2026-05-13T13:03:24+00:00

OK. I’m doing an update on a single row in a table. All fields

  • 0

OK. I’m doing an update on a single row in a table.
All fields will be overwritten with new data except for the primary key.
However, not all values will change b/c of the update.
For example, if my table is as follows:

TABLE (id int ident, foo varchar(50), bar varchar(50))

The initial value is:

id   foo   bar
-----------------
1    hi    there

I then execute UPDATE tbl SET foo = 'hi', bar = 'something else' WHERE id = 1

What I want to know is what column has had its value changed and what was its original value and what is its new value.

In the above example, I would want to see that the column “bar” was changed from “there” to “something else”.

Possible without doing a column by column comparison? Is there some elegant SQL statement like EXCEPT that will be more fine-grained than just the row?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:03:24+00:00Added an answer on May 13, 2026 at 1:03 pm

    There is no special statement you can run that will tell you exactly which columns changed, but nevertheless the query is not difficult to write:

    DECLARE @Updates TABLE
    (
        OldFoo varchar(50),
        NewFoo varchar(50),
        OldBar varchar(50),
        NewBar varchar(50)
    )
    
    UPDATE FooBars
    SET <some_columns> = <some_values>
    OUTPUT deleted.foo, inserted.foo, deleted.bar, inserted.bar INTO @Updates
    WHERE <some_conditions>
    
    SELECT *
    FROM @Updates
    WHERE OldFoo != NewFoo
    OR OldBar != NewBar
    

    If you’re trying to actually do something as a result of these changes, then best to write a trigger:

    CREATE TRIGGER tr_FooBars_Update
    ON FooBars
    FOR UPDATE AS
    BEGIN
        IF UPDATE(foo) OR UPDATE(bar)
            INSERT FooBarChanges (OldFoo, NewFoo, OldBar, NewBar)
                SELECT d.foo, i.foo, d.bar, i.bar
                FROM inserted i
                INNER JOIN deleted d
                    ON i.id = d.id
                WHERE d.foo <> i.foo
                OR d.bar <> i.bar
    END
    

    (Of course you’d probably want to do more than this in a trigger, but there’s an example of a very simplistic action)

    You can use COLUMNS_UPDATED instead of UPDATE but I find it to be pain, and it still won’t tell you which columns actually changed, just which columns were included in the UPDATE statement. So for example you can write UPDATE MyTable SET Col1 = Col1 and it will still tell you that Col1 was updated even though not one single value actually changed. When writing a trigger you need to actually test the individual before-and-after values in order to ensure you’re getting real changes (if that’s what you want).

    P.S. You can also UNPIVOT as Rob says, but you’ll still need to explicitly specify the columns in the UNPIVOT clause, it’s not magic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.