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 607691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:23:13+00:00 2026-05-13T17:23:13+00:00

I could probably google this, but it seemed quirky enough that it might be

  • 0

I could probably google this, but it seemed quirky enough that it might be worth having logged as an answer on SA.

So in development land, if you want to swap interchance the values of two variables, you need a third temp variable.

e.g.

string x = "ABC";
string y = "DEF";

string temp;

temp = x;
x = y;
y = temp;

However in a SQL Update, you can simply say

UPDATE table
SET ColumnA = ColumnB, ColumnB = ColumnA

How does this work under the hood

  • Does SQL Server take a snap shot of the entire row first ?
  • Does SQL Server take a snap shot of all the rows being updated in one go ?
  • Does the optimizer realize that it’s doing a column interchange, and make a temp variable behind the scenes?

Cheers EoinC

  • 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-13T17:23:13+00:00Added an answer on May 13, 2026 at 5:23 pm

    Does SQL Server take a snap shot of the entire row first?

    In a way, yes it does.

    This is an interesting scenario that highlights the difference between declarative and procedural code. Let us take the following example:

    UPDATE 
        users 
    SET 
        first_name = last_name, 
        last_name = first_name,
        age = 55
    WHERE
        user_id = 100
    

    The UPDATE statement would work somewhat like this:

    • First it checks the WHERE clause. All the rows that match the WHERE clause would be marked as a subset. If there were no WHERE clause, then the entire table would have been marked. Using the above example, we could have a subset like the following:
        user_id  |  first_name  |  last_name  |  age  |  country
        ---------+--------------+-------------+-------+---------
        100      |  John        |  Doe        |  50   |  USA
    
    • Then a new subset is constructed from the SET clause. Fields that are not mentioned in the SET clause are copied from the original subset.

      The age field in the new subset will be assigned the value of 55 directly. The same will happen for the first_name and last_name fields, but their new assignment values would be retrieved from the original subset. The country field is copied as-is from the original subset, since it was not mentioned in the SET clause.

        user_id  |  first_name  |  last_name  |  age  |  country
        ---------+--------------+-------------+-------+---------
        100      |  Doe         |  John       |  55   |  USA
    
    • The original subset is then deleted from the table and the new subset is inserted.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a very novice question, but I could not find an answer.
I'm sure I could google this and it's probably on SO already, but I
I could probably write this myself, but the specific way I'm trying to accomplish
I could probably find this out from somewhere else but thought it would be
Probably really easy for a pro, but could someone re-write this from it's PHP
Ok so this may be a dumb question (probably some syntax) but Google didn't
This is probably something simple I'm overlooking but my google-fu isn't turning anything up
I could probably go benchmark this myself but I'm not 100% sure how to
I realize that this question is impossible to answer absolutely, but I'm only after
This is probably really simple but I really could not word it properly on

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.