Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Order columns by column headers when dispalyed?

Author  Topic 

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2012-10-09 : 18:07:20
Hi friends,

I have a table customer with following columns A,B,C
all jumbled up

So when i use the query select * from Customer
The results show up as this

B A C but i want them to be displayed in order..is that doable?

Thank you

lazerath
Constraint Violating Yak Guru

343 Posts

Posted - 2012-10-09 : 18:33:12
Just do this:

SELECT A,B,C from Customer


In fact, you should avoid SELECT * in almost all production level code. The one case where it is acceptable is with EXISTS.
Go to Top of Page

Mike Jackson
Starting Member

37 Posts

Posted - 2012-10-10 : 08:52:41
If you have a bunch of columns you could do this....

declare @table as varchar(50)
set @table='YourTable'
declare @test as varchar(5000)
select @test= COALESCE(@test + ', ', '') + [Name]
from sys.columns
where object_id like
(
select Object_id
from sys.tables
where name=@table
)

select @test= 'Select ' + @test + ' from ' + @table

print @test

exec (@test)

MJ
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2012-10-11 : 11:34:32
Thank you lazerath and mike..Really appreciate it..
Go to Top of Page
   

- Advertisement -