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
 SQL Server Development (2000)
 dynamic sql

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-05-18 : 08:51:54
Nikhil writes "i have a table which gives me a list of months as records...
ie..columnname : month
row1: jan
row2: feb
row 3: mar

and so on..
i want to make a column..where these months appear as column name

ie...column1: jan
column2: feb
column3: mar

also...what i want is if in the first table...any month is added as a row.... the second table should also take the new month as a new column....

let me know asap

regards

Nikhil"

nr
SQLTeam MVY

12543 Posts

Posted - 2004-05-18 : 09:02:28
Trying to think of any reason to do this (similarly for the other 2 times this sort of question has come up on other sites today)
You can only have a max of 12 months so why not create them all and leave them as null.

something like this to add columns

select month into #a from monthtbl left outer join syscolumns c on c.name = monthtbl.month and c.id = object_id('coltbl') where c.name is null

declare @col varchar(128), @sql varchar(1000)
select @col = ''
while @col < (select max(month) from #a)
begin
select @col = min(month) from #a where month > @col
select @sql = 'alter table coltbl add ' + @col + ' varchar(10) null'
exec (@sql)
end



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -