往sqlserver中动态添加字段
drop table testcreate table test(id int primary key identity(1,1),name varchar(20));alter table test add clm_t1 nvarchar(20) nullalter table test drop column ['+@dateStr+']select * from test;declare @startYear int,@endYear int;declare @startMonth int,@endMonth intdeclare @dateStr nvarchar(20)declare @yearStr nvarchar(20)declare @monthStr nvarchar(20)set @startYear = 2000set @endYear=2011set @startMonth=1set @endMonth=12while(@startYear<=@endYear)beginwhile(@startMonth<=@endMonth)beginset @yearStr = convert(nvarchar,@startYear,4)if(@startMonth<10)beginset @monthStr = '0'+convert(nvarchar,@startMonth,2)endelsebeginset @monthStr = convert(nvarchar,@startMonth,2)endset @dateStr = @yearStr +'-'+@monthStrprint @dateStrexec('alter table test add ['+@dateStr+'] nvarchar(50) null')set @startMonth = @startMonth+1endset @startYear = @startYear+1print convert(nvarchar,@startYear,4)end