Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I reorder my SQL Server columns?

Tags:

sql

sql-server

I have an old table with FKs in it. I want to add a new column. I'll want to make this new column my primary key. So I thought I could either insert that column as the first one or insert it at the end of my table and re-order my columns afterwards.

But SQL Server Management Studio did not allow me to do that. I understand that I cannot do that, and that column order is almost completely irrelevant in SQL.

What I want to know, is how come... I mean, I can drop a column... isn't that the same thing as adding a new one?

I'm just trying to understand what's going on. I've had a hard time finding documentation for that also. If anyone can point me in the good direction.

like image 223
Sébastien Richer Avatar asked Dec 09 '10 19:12

Sébastien Richer


People also ask

Can you reorder columns in SQL Server?

Using SQL Server Management Studio In Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.

How do I sort a column in SQL Server?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.


2 Answers

Definitely you can. Uncheck this option in SQL Server Management Studio:
Tools > Options > Designers > Prevent saving changes that require table re-creation.

Please don't do this in production unless you know the implications!

Options dialog

like image 118
Jahan Zinedine Avatar answered Oct 07 '22 02:10

Jahan Zinedine


Reordering columns is basically the same as remaking the table, which is the workaround if changes that require recreation are disabled:

SELECT (fields in DESIRED order)
INTO MyNewTable
FROM OldTable
like image 34
JNK Avatar answered Oct 07 '22 03:10

JNK