首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

去sqlserver中动态添加字段

2012-07-24 
往sqlserver中动态添加字段drop table testcreate table test(id int primary key identity(1,1),name var

往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

热点排行