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»»

SELECT @local_variable Expand / Collapse
Author
Message
Posted Saturday, December 12, 2009 11:23 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 7:18 AM
Points: 1,369, Visits: 1,314
Comments posted to this topic are about the item SELECT @local_variable



See, understand, learn, try, use efficient
© Dr.Plch
Post #833379
Posted Monday, December 14, 2009 1:17 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:53 AM
Points: 2,528, Visits: 2,402
If the SELECT statement returns no rows, the variable retains its present value. If expression is a scalar subquery that returns no value, the variable is set to NULL.

That isn't true.
Count() return 0

create table xxx (i int identity, a varchar(3))
go
declare @txt varchar(255)set @txt = 'Question'
select @txt = count(1) from xxx order by 1
insert xxx values ('of')insert xxx values ('the')insert xxx values ('day')
select @txt = @txt + ' ' + a from xxx order by i
select @txt
Post #833639
Posted Monday, December 14, 2009 1:59 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
Carlo Romagnano (12/14/2009)

That isn't true.
Count() return 0

This is not true that "That isn't true"
"SELECT COUNT" statement from your example returns a zero as a result, so the result contains one row. Thus the condition "the SELECT statement returns no rows" fails and the variable changes its value.
Post #833653
Posted Monday, December 14, 2009 2:33 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:05 AM
Points: 2,842, Visits: 3,875
Hm, is someone able to explain this?

Same code as before, but positioned order by (1 instead of i) and "-" as separator for better readability.
SET NOCOUNT ON
create table #xxx (i int identity, a varchar(3))
go
declare @txt varchar(255)
set @txt = 'Question' select @txt = isnull (a, '?') from #xxx order by i insert #xxx values ('of')
insert #xxx values ('the')
insert #xxx values ('day') select @txt = @txt + '-' + a from #xxx order by 1
select @txt
DROP TABLE #xxx

Result:

Question-the


Looks like only the first or last (depending on DESC or ASC sort) row is used in the end result.
Is that expected behaviour?


Best Regards,
Chris Büttner
Post #833670
Posted Monday, December 14, 2009 2:49 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 7:18 AM
Points: 1,369, Visits: 1,314
Christian Buettner-167247 (12/14/2009)
Hm, is someone able to explain this?

Same code as before, but positioned order by (1 instead of i) and "-" as separator for better readability.


As said in explanation:
The second construct is not documented, but works this same way on MS SQL Server 2000, 2005, and 2008.

I don't know how the undocumented construct works. But there are another questions: What is the first column in your select? Is it OK to order on missing column?




See, understand, learn, try, use efficient
© Dr.Plch
Post #833679
Posted Monday, December 14, 2009 3:04 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:05 AM
Points: 2,842, Visits: 3,875
honza.mf (12/14/2009)
Christian Buettner-167247 (12/14/2009)
Hm, is someone able to explain this?

Same code as before, but positioned order by (1 instead of i) and "-" as separator for better readability.


As said in explanation:
The second construct is not documented, but works this same way on MS SQL Server 2000, 2005, and 2008.

I don't know how the undocumented construct works. But there are another questions: What is the first column in your select? Is it OK to order on missing column?

Hi Honza,

I know it's not documented - I just would like to know if someone knows what is going on behind the scenes. I am just curious. My question was not meant to be a challenge for your QotD.

But with regards to your question
Is it OK to order on missing column?

It "might" be OK, since SQL Server is not generating an error.
But it might as well be not OK (to which I tend more)
But take the "unsupported" SET statement from your QotD - is it OK to use it as it is? From a SQL standpoint it should not be valid, but obviously it seems to work and you can do some useful stuff with it.

Now don't ask me for what you could use the positioned order by in my example - actually i just misread your example and exchanged the I with a 1 accidentially. Took me a while to figure out what I had done wrong...


Best Regards,
Chris Büttner
Post #833686
Posted Monday, December 14, 2009 3:30 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
Christian Buettner-167247 (12/14/2009)

Same code as before, but positioned order by (1 instead of i)
select @txt = @txt + '-' + a from #xxx order by 1


Not the same code. Your code is equal to
select @txt = @txt + '-' + a from #xxx order by @txt + '-' + a

.
Looks like SQL Server doesn't use recursive assignment in this case. SQL Server cannot change the value of a variable and, at the same time, sort data using this volatile variable.
When "order by i" is used, SQL Server can assign values to the variable recursively.
This is just my guess
Post #833691
Posted Monday, December 14, 2009 3:53 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, August 29, 2014 10:20 AM
Points: 1,740, Visits: 6,366
I guessed rigtht, but only because I knew it had to begin with 'Question' and there was only one answer that did that. If there'd been an option for 'Question day' then I'd have gone for that and got it wrong

Thinking about it, if you do
select @txt = ' ' + a from xxx order by i

then the variable ends up with the value 'day'. This is presumably because it applies the assignment to each and every row, so it ends up with the value of the last one. I'd assumed that it was more 'intelligent' than this and just did the assignement once, for the last row. That is what is implied by BOL which says "If the SELECT statement returns more than one value, the variable is assigned the last value that is returned."
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/8e1a9387-2c5d-4e51-a1fd-a2a95f026d6f.htm

But if it actually does it once per row then it makes sense that
select @txt = @txt + ' ' + a from xxx order by i
should end up with a concatentation of all the values.
Post #833696
Posted Monday, December 14, 2009 4:53 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 7:18 AM
Points: 1,369, Visits: 1,314
Toreador (12/14/2009)
I guessed rigtht, but only because I knew it had to begin with 'Question' and there was only one answer that did that. If there'd been an option for 'Question day' then I'd have gone for that and got it wrong


I wanted to mix two things and not to make it too complicated.
There was a very similar QotD few weeks ago. I was afraid this one will become much more simple. And it was taken 2 points

I will think about it, if I will write some other QotD





See, understand, learn, try, use efficient
© Dr.Plch
Post #833719
Posted Monday, December 14, 2009 5:00 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 7:18 AM
Points: 1,369, Visits: 1,314
Christian Buettner-167247 (12/14/2009)

Hi Honza,

I know it's not documented - I just would like to know if someone knows what is going on behind the scenes. I am just curious. My question was not meant to be a challenge for your QotD.

But with regards to your question
Is it OK to order on missing column?

It "might" be OK, since SQL Server is not generating an error.
But it might as well be not OK (to which I tend more)
But take the "unsupported" SET statement from your QotD - is it OK to use it as it is? From a SQL standpoint it should not be valid, but obviously it seems to work and you can do some useful stuff with it.

Now don't ask me for what you could use the positioned order by in my example - actually i just misread your example and exchanged the I with a 1 accidentially. Took me a while to figure out what I had done wrong...


I don't know what is going on behind the scenes.
Sometimes I do some type of mental reverse engineering, just trying "How do I programme this".
This additive assignment to local variables I feel to be an implicit cursor. I suppose the server creates something like a cursor and makes an assignment in it. I haven't compared yet the efficiency of these two variants (single select assignment and cursor). At least it is less coding.




See, understand, learn, try, use efficient
© Dr.Plch
Post #833725
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse