Is this syntax guaranteed to continue working?

  • Hi all

    Pleas check out the code bellow:

    declare @tbl table (i int)

    declare @i int

    insert into @tbl (i) values (1)

    update @tbl

    set @i = i, i = 2

    select @i as varible, i from @tbl

    This code stores the column's old value in a variable and then modifies the column's value. I know that code like that has been working for years, but I don't know if it is something that could be modified in the future. If an application has this kind of code, could it break with the next service pack or next SQL Server version or is this kind of code something that should always work?

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • This is known as the all-at-once concept in SQL Server.

    All-At-Once Operations

    This should continue to work in next versions of SQL Server.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Nothing is certain but death and taxes

    You should hope that it will work for ever.

    Right now, there is nothing in MS BoL which suggests that they might change this bahaviour:

    http://msdn.microsoft.com/en-us//library/ms177523.aspx

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Thank you for your replies. I appreciate your fast help.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Adi Cohn-120898 (7/8/2013)


    Hi all

    Pleas check out the code bellow:

    declare @tbl table (i int)

    declare @i int

    insert into @tbl (i) values (1)

    update @tbl

    set @i = i, i = 2

    select @i as varible, i from @tbl

    This code stores the column's old value in a variable and then modifies the column's value. I know that code like that has been working for years, but I don't know if it is something that could be modified in the future. If an application has this kind of code, could it break with the next service pack or next SQL Server version or is this kind of code something that should always work?

    Adi

    It sounds like you're scoping out a project, Adi. What do you actually need this for? I ask because what you've posted makes up the "guts" of what has become endearingly known as the "Quirky Update" which, BTW, can also take the form of SET @variable = column = expression when used with some care.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff – Sorry for disappearing, but for some reason I missed your response. What I wanted to do was very simple. I was working on improving performance on a stored procedure. One of the changes that I've made was to make fewer data access statements. In the procedure there was a code that looked like that:

    Select @OldValue = Col1 from MyTable where PK=X

    Update MyTable SET Col1 = NewValue where PK=X

    I wanted to access the table only once instead of twice.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Adi Cohn-120898 (7/9/2013)


    Jeff – Sorry for disappearing, but for some reason I missed your response. What I wanted to do was very simple. I was working on improving performance on a stored procedure. One of the changes that I've made was to make fewer data access statements. In the procedure there was a code that looked like that:

    Select @OldValue = Col1 from MyTable where PK=X

    Update MyTable SET Col1 = NewValue where PK=X

    I wanted to access the table only once instead of twice.

    Adi

    You are right to make such an improvement. The old code is not efficient.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Why not use the OUTPUT clause?


    N 56°04'39.16"
    E 12°55'05.25"

  • Adi Cohn-120898 (7/9/2013)


    Jeff – Sorry for disappearing, but for some reason I missed your response. What I wanted to do was very simple. I was working on improving performance on a stored procedure. One of the changes that I've made was to make fewer data access statements. In the procedure there was a code that looked like that:

    Select @OldValue = Col1 from MyTable where PK=X

    Update MyTable SET Col1 = NewValue where PK=X

    I wanted to access the table only once instead of twice.

    Adi

    The problem with that is that it will only handle one row at a time. I agree that's the way you designed it with the PK=X but what is the larger picture? Is this something that you need to do with a batch of rows?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/20/2013)


    Adi Cohn-120898 (7/9/2013)


    Jeff – Sorry for disappearing, but for some reason I missed your response. What I wanted to do was very simple. I was working on improving performance on a stored procedure. One of the changes that I've made was to make fewer data access statements. In the procedure there was a code that looked like that:

    Select @OldValue = Col1 from MyTable where PK=X

    Update MyTable SET Col1 = NewValue where PK=X

    I wanted to access the table only once instead of twice.

    Adi

    The problem with that is that it will only handle one row at a time. I agree that's the way you designed it with the PK=X but what is the larger picture? Is this something that you need to do with a batch of rows?

    It will always be for 1 record. If this would have been with more then few records, I would have used output clause with temporary table.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • SwePeso (7/20/2013)


    Why not use the OUTPUT clause?

    Output clause works can output the data to the screen or to a table. It can't output the data into a variable. Since in this case I will always get only 1 column and only 1 record, I don't want to use temporary tables.

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply