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.
| Author |
Topic |
|
avijit_mca
Posting Yak Master
109 Posts |
Posted - 2009-01-05 : 07:47:32
|
| create table junk (id varchar(20),name varchar(50))ALTER TABLE junkADD cola nvarchar(299) NULLIs it possible to create column with variable.My aim :declare @i as intdeclare @x as varchar(3)set @x='aa'+@iset @i=1while @i<=10begin ALTER TABLE junkADD @x nvarchar(299) NULL -- Error endActually i want to create column run time. bcoz no of column are not fixed .. its depend on no of days in months.so if no of months is 30 then i need 30 + 2 (fixed column)column.if u have any good idea pls share with me.Regards,AvijitRegards,avijit |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-05 : 07:49:40
|
| use dynamic sql. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-01-05 : 07:56:15
|
On a different note, I see some problems there in your code. You are missing the increment to @i and change to @x.I'd use something like this.declare @i as varchar(3)declare @x as varchar(3)set @x='aa'+@iset @i=1while @i<=2begin set @x='aa'+@iexec ('ALTER TABLE junk ADD '+@x+' nvarchar(299) ')set @i=@i+1end |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-05 : 08:57:31
|
quote: Originally posted by avijit_mca create table junk (id varchar(20),name varchar(50))ALTER TABLE junkADD cola nvarchar(299) NULLIs it possible to create column with variable.My aim :declare @i as intdeclare @x as varchar(3)set @x='aa'+@iset @i=1while @i<=10begin ALTER TABLE junkADD @x nvarchar(299) NULL -- Error endActually i want to create column run time. bcoz no of column are not fixed .. its depend on no of days in months.so if no of months is 30 then i need 30 + 2 (fixed column)column.if u have any good idea pls share with me.Regards,AvijitRegards,avijit
why do you want to generate columns like this? if your reqmnt is to get monthwise totals of something, cant you use pivot oir similar crosstabbing options? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-06 : 07:37:41
|
quote: Originally posted by visakh16
quote: Originally posted by avijit_mca create table junk (id varchar(20),name varchar(50))ALTER TABLE junkADD cola nvarchar(299) NULLIs it possible to create column with variable.My aim :declare @i as intdeclare @x as varchar(3)set @x='aa'+@iset @i=1while @i<=10begin ALTER TABLE junkADD @x nvarchar(299) NULL -- Error endActually i want to create column run time. bcoz no of column are not fixed .. its depend on no of days in months.so if no of months is 30 then i need 30 + 2 (fixed column)column.if u have any good idea pls share with me.Regards,AvijitRegards,avijit
why do you want to generate columns like this? if your reqmnt is to get monthwise totals of something, cant you use pivot oir similar crosstabbing options?
Yes. It seems OP needs PIVOT onlyMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|