Solved

How to rearrange column in a sql table by query

Posted on 2016-07-19
12
67 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
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.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

773 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