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 2000 Forums
 Transact-SQL (2000)
 Dynamic Columns in Temp Table?

Author  Topic 

krugg
Starting Member

12 Posts

Posted - 2006-10-16 : 19:39:07
Hi,
Can't anyone tell me why this won't work? I've turned google upside down on this one. I need to dynamically add columns to a table inside a stored procedure.

create table basicTable (ID INT IDENTITY)

declare @colTwo char(10)

set @colTwo = 'houseType'

alter table basicTable
ADD @colTwo int null

Thanks
Krugg

krugg
Starting Member

12 Posts

Posted - 2006-10-16 : 19:50:33
cancel that!

set @sql = 'alter table basicTable
ADD '+@colTwo+' int null'

exec(@sql)

that was easy!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-16 : 21:09:50
Why do you want to add columns dynamically?

Madhivanan

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

- Advertisement -