How to rearrange column in a sql table by query

Hello,

How do I write a query to rearrange a column in table.
RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
In SQL Server this is not possible, either manually or within a query.  

Microsoft Access has this functionality where in the table designer you can click-and-drag columns to change the order, but SQL Server does not.

What you can do is either backup/drop/re-create/re-populate the table with the new column order, or create a view that selects from your table in the column order you wish.
1
Trideep PatelProject LeadCommented:
Do you want to sort the output of the query based on some column(s)?

In such case you can use "order by"
0
Éric MoreauSenior .Net ConsultantCommented:
what do you mean? You to really change the order in the definition of the table? For that you need to create a new table with columns in the correct order, copy all the data from the existing table to the new one, drop the existing table and finally rename the new one. After that, you will also need to recreate all indexes.

Or another way is to use the SSMS table designer (which will do all the script for you).
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

RIASAuthor Commented:
If not then is there any way i can insert a column in a table before another column ?
0
Trideep PatelProject LeadCommented:
You can do it using the design view in SSMS (Sql Server Management Studio).
0
Éric MoreauSenior .Net ConsultantCommented:
open the table designer in SSMS, select one of the column and click the INSERT key. That will insert a new column in between
0
Trideep PatelProject LeadCommented:
InsertColumn.jpg
1
RIASAuthor Commented:
need to have it in query ,please.
Cheers
0
Éric MoreauSenior .Net ConsultantCommented:
Your best way of doing it is to use the SSMS table designer to add the column but before saving it, right click on one of the column in the designer and click the "Generate change script..." option. The full script will be created for you automatically.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RIASAuthor Commented:
Éric Moreau,

Need to create a stored procedure ,so cant touch the SSMS.
0
Éric MoreauSenior .Net ConsultantCommented:
I am not saying to save using the SSMS table designer, just to use that tool to generate the script you need!
0
RIASAuthor Commented:
Cheers!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.