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 Monday, December 14, 2009 10:27 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 10:18 AM
Points: 17,809, Visits: 15,731
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.
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?


Interesting, I see the same results when using the "order by 1" in the variable assignment.

In SSMS 2008, the query will run but it throws a warning prior to execution.

"The Order By position number 1 is out of range of the number of items in the select list."

I believe the results being returned are due to the out of range error.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #833984
Posted Monday, December 14, 2009 1:17 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 8:58 AM
Points: 145, Visits: 192
I find the variable assignment rather interesting in that it seems to occur at a curious point. To see what happened when there is a null value, I tried:

declare @txt varchar(255)
set @txt = 'Question'
insert xxx values ('')
select @txt = isnull (a, '?') from xxx order by i

and got a null string and not "?" as I was expecting.

Does you know why? It seems to me that the variable assignment occurs at a rather odd place in the execution of the statement.
Post #834118
Posted Monday, December 14, 2009 1:53 PM
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
Richard Gibbins (12/14/2009)
To see what happened when there is a null value, I tried:
declare @txt varchar(255)
set @txt = 'Question'
insert xxx values ('')
select @txt = isnull (a, '?') from xxx order by i

and got a null string and not "?" as I was expecting.

In SQL Server, an empty string and a NULL value are not the same.

It can be shown by this:
create table #xxx (i int identity, a varchar(3))
declare @txt varchar(255)
set @txt = 'Question'
insert #xxx values ('')
select @txt = isnull (a, '?') from #xxx order by i
select @txt -- the result is '' (empty string)
insert #xxx values (null)
select @txt = isnull (a, '?') from #xxx order by i
select @txt -- the result is '?'
select @txt = a from #xxx order by i
select @txt -- the result is NULL
drop table #xxx

and this:
declare @var varchar(255)
set @var = ''
if @var is null select 'NULL' else select 'NOT NULL' -- the result is 'NOT NULL'
set @var = null
if @var is null select 'NULL' else select 'NOT NULL' -- the result is 'NULL'

As far as I know, NULLs and empty strings are equal in Oracle. But in SQL Server they are not.
Post #834138
Posted Monday, December 14, 2009 1:58 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 8:58 AM
Points: 145, Visits: 192
Ah! Good catch on Oracle vs Sql Server; I didn't realize it was that obvious.

Thanks for the explanation; it makes more sense than what I was imagining.

Richard
Post #834139
Posted Monday, December 14, 2009 2:19 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, September 22, 2014 1:20 AM
Points: 1,387, Visits: 1,315
vk-kirov (12/14/2009)

In SQL Server, an empty string and a NULL value are not the same.

As far as I know, NULLs and empty strings are equal in Oracle. But in SQL Server they are not.


To distinguish NULLs and empty strings or to find padding spaces I use
select '«' + @txt + '»'

The difference is visible at the first look. You can use some other type of quotes or brackets if you like.

Sorry to oraclists, I am used to MS SQL Server.





See, understand, learn, try, use efficient
© Dr.Plch
Post #834152
Posted Saturday, March 20, 2010 2:07 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, June 13, 2014 3:03 PM
Points: 594, Visits: 655
I got lucky and guessed the one with all of the text... :)

I am not seeing why the concatenation of the inserts occurs... can someone break this down further??


Peter Trast
Microsoft Certified ...(insert many literal strings here)
Microsoft Design Architect with Alexander Open Systems
Post #886959
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse