Link to home
Start Free TrialLog in
Avatar of Lawrence Avery
Lawrence AveryFlag for United States of America

asked on

Sql Server Table Column positioning

I added a column to my Sql Server Table by using T-SQL. However, I want to know can I reposition an existing column. In other words, I want to move my column I added to Column 1 position.  Can I do that using T-SQL.?
ASKER CERTIFIED SOLUTION
Avatar of Gregory Miller
Gregory Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am pretty certain that you must create a new table with the columns in the order you want and then copy the data from the old table to the newly created table. Then drop the old table to make this work.
Avatar of Snarf0001
In the end it does definitely have to be done by creating a new table, but it's still all going to be through tsql...

You can do this in Management Studio, if you open a table in design view and insert the column, you can hit the button for "Generate Change Script", which will give you the TSQL commands that it's going to run in order to make the change.

Gives you a better idea of what's involved.
Note that depending on how many indexes / FKs linking to the table, that change script could be huge.  As it's going to have to drop all of the constraints and indexes, transfer the data, and then recreate.