Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to rearrange column in a sql table by query

Posted on 2016-07-19
12
Medium Priority
?
462 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

618 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