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
 General SQL Server Forums
 New to SQL Server Programming
 alter table issue

Author  Topic 

pnpsql
Posting Yak Master

246 Posts

Posted - 2011-12-12 : 02:29:15
I need to add 10 columns in a table, but i don't know if any column i need to add already exist in table.

when i add all ten in one alter statement , and a column that already exists found then rest 9 not get add.

pls help me..

i don't want to use 10 alter statements.



challenge everything

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-12-12 : 04:55:01
If any of the columns exists all will not be added. Only way is to write seperate alter table statement to add columns

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sunnyleoneo
Starting Member

19 Posts

Posted - 2011-12-12 : 06:15:23
You can check whether column name exist in you table or not by following code
if Exists(select * from sys.columns where Name = N'columnName'
and Object_ID = Object_ID(N'tableName'))

begin

-- Column Exists

end
Go to Top of Page
   

- Advertisement -