Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

How to rearrange column in a sql table by query

Hello,

How do I write a query to rearrange a column in table.
0
RIAS
Asked:
RIAS
  • 4
  • 4
  • 3
  • +1
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
Trideep PatelCommented:
Do you want to sort the output of the query based on some column(s)?

In such case you can use "order by"
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
RIASAuthor Commented:
If not then is there any way i can insert a column in a table before another column ?
0
 
Trideep PatelCommented:
You can do it using the design view in SSMS (Sql Server Management Studio).
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
Trideep PatelCommented:
InsertColumn.jpg
1
 
RIASAuthor Commented:
need to have it in query ,please.
Cheers
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
RIASAuthor Commented:
Éric Moreau,

Need to create a stored procedure ,so cant touch the SSMS.
0
 
Éric MoreauSenior .Net ConsultantCommented:
I am not saying to save using the SSMS table designer, just to use that tool to generate the script you need!
0
 
RIASAuthor Commented:
Cheers!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now