Solved

How to rearrange column in a sql table by query

Posted on 2016-07-19
12
235 Views
Last Modified: 2016-07-19
Hello,

How do I write a query to rearrange a column in table.
0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41719200
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
 
LVL 9

Expert Comment

by:Trideep Patel
ID: 41719204
Do you want to sort the output of the query based on some column(s)?

In such case you can use "order by"
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41719206
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:RIAS
ID: 41719209
If not then is there any way i can insert a column in a table before another column ?
0
 
LVL 9

Expert Comment

by:Trideep Patel
ID: 41719211
You can do it using the design view in SSMS (Sql Server Management Studio).
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41719214
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
 
LVL 9

Expert Comment

by:Trideep Patel
ID: 41719218
InsertColumn.jpg
1
 

Author Comment

by:RIAS
ID: 41719241
need to have it in query ,please.
Cheers
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41719247
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
 

Author Comment

by:RIAS
ID: 41719263
Éric Moreau,

Need to create a stored procedure ,so cant touch the SSMS.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41719285
I am not saying to save using the SSMS table designer, just to use that tool to generate the script you need!
0
 

Author Closing Comment

by:RIAS
ID: 41719356
Cheers!
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question