How to rearrange column in a sql table by query


How do I write a query to rearrange a column in table.
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 HornSQL Server Data DudeCommented:
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.
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"
É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).
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

RIASAuthor Commented:
If not then is there any way i can insert a column in a table before another column ?
Trideep PatelProject LeadCommented:
You can do it using the design view in SSMS (Sql Server Management Studio).
É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
Trideep PatelProject LeadCommented:
RIASAuthor Commented:
need to have it in query ,please.
É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.

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.
É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!
RIASAuthor Commented:
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.