How to change column order in a table using SQL query in SQL Server 2005?
I want to rearrange column order in a table using SQL query.
How to change column order in a table using SQL query in SQL Server 2005?
I want to rearrange column order in a table using SQL query.
In SQLServer Management Studio:
Tools -> Options -> Designers -> Table and Database Designers
Unselect Prevent saving changes that require table re-creation.
Now you can reorder the table.
If you have not yet added any data into your table yet, there is one way to move the columns around. Try this:
Optional: 3. Re-enable the checkbox for the option from Step 1 to re-secure your table.
Hope this helps someone!
Credit goes to Microsoft: https://docs.microsoft.com/en-us/troubleshoot/sql/ssms/error-when-you-save-table#more-information
Not sure if still relevant, but SSMS can generate a change scripts for this.
The generated script contains the script which does the following:
If the columns to be reordered have recently been created and are empty, then the columns can be deleted and re-added in the correct order.
This happened to me, extending a database manually to add new functionality, and I had missed a column out, and when I added it, the sequence was incorrect.
After finding no adequate solution here I simply corrected the table using the following kind of commands.
ALTER TABLE tablename DROP COLUMN columnname;
ALTER TABLE tablename ADD columnname columntype;
Note: only do this if you don't have data in the columns you are dropping.
People have said that column order does not matter. I regularly use SQL Server Management Studio "generate scripts" to create a text version of a database's schema. To effectively version control these scripts (git) and to compare them (WinMerge), it is imperative that the output from compatible databases is the same, and the differences highlighted are genuine database differences.
Column order does matter; but just to some people, not to everyone!
In order to have a specific column order You need to select column by column in the order You wish. Selection order dictates how columns will be ordered in output.
Try this command:
alter table students modify age int(5) first;
This will change the position of age to the first position.