SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Is this a bug ???


Is this a bug ???

Author
Message
Saurv
Saurv
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 580
why is the difference between both outputs ...

--SQL1:


Declare @tab table (seq int , k varchar(20))

declare @comma_Delimited varchar(max)
set @comma_delimited=''

insert into @tab
select 1,'s'
union all
select 2,'K'
union all
select 3,'t'
union all
select 4,'l'

select @comma_Delimited =@comma_Delimited + k + ',' from @tab order by 1

select @comma_Delimited


--SQL2.


Declare @tab table (seq int , k varchar(20))

declare @comma_Delimited varchar(max)
set @comma_delimited=''

insert into @tab
select 1,'s'
union all
select 2,'K'
union all
select 3,'t'
union all
select 4,'l'


select @comma_Delimited =@comma_Delimited + k + ',' from @tab order by seq

select @comma_Delimited


Why "Order by 1" is creating difference ...?
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5086 Visits: 5478
It's not a bug.
The syntax you are using is never reliable to get string concatenation in T-SQL.
BTW, Order By 1 in your first sample, is not inequivalent to Order By seq... It is equal to

order by @comma_Delimited + k + ','



_____________________________________________
"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
Saurv
Saurv
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 580
ok......Got it thanks....:-)
GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24001 Visits: 9730
If you want to generate a list of values from a column of values, like this, your best bet is to use For XML Path to do the work for you.

Bing/Google/whatever "concatenate string sql server for xml" or "sql server for xml path", and you'll find articles and tutorials on how to do this. It's very fast, effective, and quite a neat trick.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search