Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Is this syntax guaranteed to continue working? Expand / Collapse
Author
Message
Posted Monday, July 8, 2013 6:02 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:34 AM
Points: 2,109, Visits: 5,420
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/
Post #1471132
Posted Monday, July 8, 2013 6:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 11:26 AM
Points: 13,622, Visits: 10,514
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.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1471143
Posted Monday, July 8, 2013 6:28 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 7:27 AM
Points: 2,836, Visits: 5,067
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1471144
Posted Monday, July 8, 2013 7:13 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:34 AM
Points: 2,109, Visits: 5,420
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/
Post #1471173
Posted Monday, July 8, 2013 2:39 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:11 AM
Points: 36,995, Visits: 31,516
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1471368
Posted Tuesday, July 9, 2013 9:31 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:34 AM
Points: 2,109, Visits: 5,420
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/
Post #1471936
Posted Wednesday, July 10, 2013 1:44 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 11:26 AM
Points: 13,622, Visits: 10,514
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.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1472330
Posted Saturday, July 20, 2013 10:27 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 12:48 PM
Points: 2,397, Visits: 3,406
Why not use the OUTPUT clause?



N 56°04'39.16"
E 12°55'05.25"
Post #1475760
Posted Saturday, July 20, 2013 1:47 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:11 AM
Points: 36,995, Visits: 31,516
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1475768
Posted Monday, July 22, 2013 12:18 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:34 AM
Points: 2,109, Visits: 5,420
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/
Post #1476195
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse