Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To change the column order of An Existing Table in SQL Server 2008

Tags:

sql-server

I have situation where I need to change the order of the columns/adding new columns for existing Table in SQL Server 2008.

Existing column

MemberName MemberAddress Member_ID(pk) 

and I want this order

Member_ID(pk) MemberName MemberAddress 
like image 238
Aman Avatar asked Apr 03 '13 07:04

Aman


People also ask

How do you change the order of columns?

Press and hold the Shift key, and then drag the column to a new location. You will see a faint "I" bar along the entire length of the column and a box indicating where the new column will be moved. That's it! Release the mouse button, then leave the Shift key and find the column moved to a new position.

How do I reorder a table in SQL?

You can change the order of the rows by adding an ORDER BY clause at the end of your query, with a column name after. By default, the ordering will be in "ascending order", from lowest value to highest value. To change that to "descending order", specify DESC after the column name.

How do you change the structure of an existing table?

The SQL ALTER TABLE command is used to change the structure of an existing table. It helps to add or delete columns, create or destroy indexes, change the type of existing columns, or rename columns or the table itself. It can also be used to change the comment for the table and type of the table.


Video Answer


2 Answers

I got the answer for the same , Go on SQL Server → Tools → Options → Designers → Table and Database Designers and unselect Prevent saving changes that require table re-creation

enter image description here

2- Open table design view and that scroll your column up and down and save your changes.

enter image description here

like image 111
Aman Avatar answered Oct 13 '22 11:10

Aman


It is not possible with ALTER statement. If you wish to have the columns in a specific order, you will have to create a newtable, use INSERT INTO newtable (col-x,col-a,col-b) SELECT col-x,col-a,col-b FROM oldtable to transfer the data from the oldtable to the newtable, delete the oldtable and rename the newtable to the oldtable name.

This is not necessarily recommended because it does not matter which order the columns are in the database table. When you use a SELECT statement, you can name the columns and have them returned to you in the order that you desire.

like image 45
Venki Avatar answered Oct 13 '22 12:10

Venki