How do I write a query to rearrange a column in table.
Microsoft SQL Server 2005Microsoft SQL ServerMicrosoft SQL Server 2008
Last Comment
RIAS
8/22/2022 - Mon
Jim Horn
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.
Trideep Patel
Do you want to sort the output of the query based on some column(s)?
In such case you can use "order by"
Éric Moreau
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).
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-p