July 8, 2013 at 6:02 am
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/
July 8, 2013 at 6:28 am
This is known as the all-at-once concept in SQL Server.
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
July 8, 2013 at 6:28 am
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:
July 8, 2013 at 7:13 am
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/
July 8, 2013 at 2:39 pm
Adi Cohn-120898 (7/8/2013)
Hi allPleas 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
Change is inevitable... Change for the better is not.
July 9, 2013 at 9:31 pm
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/
July 10, 2013 at 1:44 pm
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
July 20, 2013 at 10:27 am
Why not use the OUTPUT clause?
N 56°04'39.16"
E 12°55'05.25"
July 20, 2013 at 1:47 pm
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
Change is inevitable... Change for the better is not.
July 22, 2013 at 12:18 pm
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/
July 22, 2013 at 12:20 pm
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