SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 column on first row
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

peace
Constraint Violating Yak Guru

404 Posts

Posted - 04/03/2013 :  10:52:15  Show Profile  Reply with Quote
i have done this:

select name,age,gender,status,2 as ordering
into #A
from tableA
UNION ALL
select 'name','age','gender','status',1 as ordering
order by ordering asc

select name+'|'+age+'|'+gender+'|'+status [output]
into #B
from #A

select output from #B

i would like the output shows the column name first instead of the result. already add the ordering.

but the result doesnt show the column at the first row.

russell
Pyro-ma-ni-yak

USA
5072 Posts

Posted - 04/03/2013 :  10:58:19  Show Profile  Visit russell's Homepage  Reply with Quote
select name,age,gender,status,2 as ordering
into #A
from tableA
UNION ALL
select 'name','age','gender','status',1 as ordering


select name+'|'+age+'|'+gender+'|'+status [output]
--into #B
from #A
order by ordering asc
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3575 Posts

Posted - 04/03/2013 :  11:03:05  Show Profile  Reply with Quote
You have to add the ordering clause to your final query. When you save the data into a table (in this case table #A and subsequently table #B), the data is stored with no consideration to ordering. Data in a SQL table is an unordered collection. The ONLY way to guarantee ordering in the output is to add the order by clause into your final select.
select name+'|'+age+'|'+gender+'|'+status [output],ordering
into #B
from #A

select output from #B order by ordering;
Go to Top of Page

peace
Constraint Violating Yak Guru

404 Posts

Posted - 04/03/2013 :  11:04:46  Show Profile  Reply with Quote
but i need to filter those not null:

.....

select output from #B
where output is not null

here is where the ordering not in order
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3575 Posts

Posted - 04/03/2013 :  11:27:51  Show Profile  Reply with Quote
Then you should add the order by clause in that select.
select output from #B
where output is not null
order by ordering
You always HAVE to indicate the ordering in the final select. Doing it earlier in the process would not help.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000