Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 column ordinal in a table

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-08-26 : 04:43:48
Hi,
How is it possible to set the column ordinal i.e. position of a new column when using the alter table statement?
It seems when using alter table add... it adds the column at the end of the existing columns. I would like to also set the position of the column being added to.
How is this done please?

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-08-26 : 04:44:08
No.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-08-26 : 04:44:53
I see.
Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-26 : 11:33:25
can i ask the need of this requirement? you can always retrieve column in order you want right?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-27 : 01:56:32
"I would like to also set the position of the column being added to.
How is this done please?
"

Create new temporary table with all columns, in desired order
INSERT data into new temporary table, from old table.
DROP old table
RENAME new temporary table to old table

plus some messing around with dropping and recreating constraints / FKeys / indexes etc.

Use the GUI in SSMS. Add the column at the bottom and DRAG it to the desired position - or click on a column and INSERT a blank row where you want your new column.

Then use the GENERATE SCRIPT button to give you a script which will do all the steps above

Personally I like to have the columns in my tables grouped by any common theme - rather than adding a new column miles away at the end of the table.
Go to Top of Page
   

- Advertisement -