Solved

How to rearrange column in a sql table by query

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

How do I write a query to rearrange a column in table.
0
Comment
Question by:RIAS
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 65

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

820 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