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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:02:28+00:00 2026-06-18T22:02:28+00:00

Given the following example: declare @i int select @i = 1, @i = 2

  • 0

Given the following example:

declare @i int
select @i = 1, @i = 2
select @i

Will @i always be 2?

This is about the most trivial example I can think of, but I am considering using this for swapping values in variables. I also believe this method of assignment (select) is not ANSI compliant (however useful), but don’t really care to have portable code in this case.

UPDATE

Thanks to @MichaelFredrickson, we have @MartinSmith’s answer and reference to MSDN on this. I am now struggling with what the second sentence in this documentation means, exactly (emphasis added):

If there are multiple assignment clauses in a single SELECT statement, SQL Server does not guarantee the order of evaluation of the expressions. Note that effects are only visible if there are references among the assignments.

The first sentence is plenty enough to keep me away from relying upon the behavior, however.

  • 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-06-18T22:02:29+00:00Added an answer on June 18, 2026 at 10:02 pm

    For variable assignment, Martin Smith answers this question here referencing MSDN:

    If there are multiple assignment clauses in a single SELECT statement,
    SQL Server does not guarantee the order of evaluation of the
    expressions. Note that effects are only visible if there are
    references among the assignments.

    But…

    If we’re dealing with tables, instead of with variables, it is a different story.

    In this case, Sql Server uses an All-At-Once operation, as discussed by Itzik Ben-Gan in T-Sql Fundamentals.

    This concept states that all expressions in the same logical phase are evaluated as if the same point in time… regardless of their left-to-right position.

    So when dealing with the corresponding UPDATE statement:

    DECLARE @Test TABLE (
        i INT,
        j INT
    )
    
    INSERT INTO @Test VALUES (0, 0)
    
    UPDATE @Test
    SET
        i = i + 10,
        j = i
    
    SELECT 
        i, 
        j 
    FROM 
        @Test
    

    We get the following results:

    i           j
    ----------- -----------
    10          0   
    

    And by using the All-At-Once evaluation… in Sql Server you can swap column values in a table without an intermediate variable / column.

    Most RBDMSs behave this way as far as I know, but MySql is an exception.


    EDIT:

    Note that effects are only visible if there are references among the
    assignments.

    I understand this to mean that if you have a SELECT statement such as the following:

    SELECT
        @A = ColumnA,
        @B = ColumnB,
        @C = ColumnC
    FROM MyTable
    

    Then it doesn’t matter what order the assignments are performed in… you’ll get the same results no matter what. But if you have something like…

    SELECT
        @A = ColumnA,
        @B = @A,
        @C = ColumnC
    FROM MyTable
    

    There is now a reference among the assignments (@B = @A), and the order that @A and @B are assigned now matters.

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

Sidebar

Related Questions

This is just an example, but given the following model: class Foo(models.model): bar =
How can you sort the following word by the given rule? Example data is
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
I want to Make selenium script which move slider given on following site Example
I was following the example given in the OpenCV for video displaying, just took
In the following contrived example, data is given a warning due to it being
Given a group of files with the following naming convention: datetime_restofname.txt an example of
This is more of a question to satisfy my own curiosity. Given the following

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.