Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 R2 Add column into a specific location

I am using SQL Server 2008 R2 and I wish to add a new column into a specific spot in the column order in a table and not at the end. I wish to avoid having to recreate the table. How do I do this without recreating the table?

i.e.

Table: Bob
==========
   Col1
   Col2

Add new Column.

Table: Bob
==========
   Col1
   NewCol
   Col2
like image 777
GordyII Avatar asked Feb 15 '11 02:02

GordyII


People also ask

How do I add a column to a specific position in SQL Server 2008?

To add a column at a specific position within a table row, use FIRST or AFTER col_name . The default is to add the column last. You can also use FIRST and AFTER in CHANGE or MODIFY operations to reorder columns within a table.

How do I add a column to a specific position in SQL?

Select all the columns into a temp table, and create a new table with the new column you want. Then drop the old table, select all the columns from the temp table, and insert them into the new table with the reordered column. No data is lost. Save this answer.

Can we specify the position where a new column has to be added?

Answer. Yes, you can add a new column in a specified position into a dataframe, by specifying an index and using the insert() function. By default, adding a column will always add it as the last column of a dataframe. This will insert the column at index 2, and fill it with the data provided by data .

How do you add a column in the middle of a table in SQL Server?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.


2 Answers

You cannot. Column are always added at the end of the column list. The only way to change the order is to recreate the table from scratch.

That being said, it should never ever matter to you what is the actual physical order of the columns, nor the logical order of column definitions. If you have dependencies on column order, your code is broken. If you expect performance gains from column order, those are myths.

like image 119
Remus Rusanu Avatar answered Sep 29 '22 11:09

Remus Rusanu


Don't do it. This goes together with the advise to never use SELECT *, in which case since you are listing all columns why does it matter their internal order?

That said, if you absolutely must, here's a demo that doesn't drop the table. The columns have to be dropped though, since you can't insert in the middle.

create table BOB(col1 int, colspace int, col2 int, col3 varchar(10))
insert BOB values (1,3, 2,'test')
;
alter table BOB add col2_copy int, col3_copy varchar(10), NewCol datetime
;
update BOB set col2_copy = col2, col3_copy = col3
;
alter table BOB drop column col2
alter table BOB drop column col3
;
alter table BOB add col2 int, col3 varchar(10)
;
update BOB set col2 = col2_copy, col3 = col3_copy
;
alter table BOB drop column col2_copy
alter table BOB drop column col3_copy
;

select * from BOB

It becomes significantly more difficult once you have constraints and defaults involved.

like image 42
RichardTheKiwi Avatar answered Sep 29 '22 11:09

RichardTheKiwi