Tuesday, March 6, 2012

Add new column?

How do you add a new column in a table between two existing columns (like when you add columns with enterprise manager) using alter table?
/y0d4Is this a slow forum or is the topic to trivial for people to open it?

/y0d4|||An ATLER TABLE statement only adds the column to the end and since this is a relational database there is no problem where the column appears. However if you wish to add the column in a partical position then:

Example

CREATE TblA
(
col1 int,
col3 int,
col4 int
)

exec sp_rename Tb1A, OldTb1A
go

CREATE TblA
(
col1 int,
col2 int,
col3 int,
col4 int
)
GO
INSERT Tb1A (col1,col2,col3 ,col4 )
SELECT col1, 0,col3 ,col4
FROM OldTb1A
GO

DROP TABLE OldTb1A
GO

Do get a better idea of handling IDENTITY and so forth columns turn Profiler on and go into Enterprise Manager under the design Table option and insert a column into the middle of table and Save. You will see all the transactions that Microsoft does to perform this in Profiler.

No comments:

Post a Comment