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)
 need sample sp code to add column

Author  Topic 

FriendOfGhost
Starting Member

9 Posts

Posted - 2010-10-09 : 18:07:51
I need a sample code to check the fields in a table and add this column if not exists. then set the value of this column. I'm not too good at sql programming and I really stuck.

any help please ?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-09 : 18:10:46
[code]
if not exists (select * from information_schema.columns where table_name = 'Table1' and column_name = 'Column1')
begin
alter table Table1 add Column1 varchar(50) null
update Table1 set Column1 = 'test'
end
[/code]

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

FriendOfGhost
Starting Member

9 Posts

Posted - 2010-10-09 : 18:19:53
that was really fast... thank you my friend. you just saved my day.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-09 : 18:24:18
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -