Solved

How to rearrange column in a sql table by query

Posted on 2016-07-19
12
33 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 69

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
 

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 69

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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 69

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 69

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore 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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now