create table #t1(a int)
declare @i int,@sql as varchar(100)
set @i = 0
while @i <= 3
begin
set @sql = 'alter table #t1 add b'+ cast(@i as char(1)) +' int'
exec(@sql)
set @i = @i +1
end
Select * from #t1
a b0 b1 b2 b3
----------- ----------- ----------- ----------- ----------- (所影响的行数为 0 行)
create table #t(id int)--动态增加10列
declare @i int,@sql varchar(1000)
set @i=1
while @i<=10
begin
set @sql='alter table #t add 列'+cast(@i as varchar)+' int'
exec(@sql)
set @i=@i+1
end--显示结果
select * from #tdrop table #t
非常感谢,接分
ALTER TABLE table2 ADD row_id bigint--删
ALTER TABLE table2 DROP COLUMN row_id--改
ALTER TABLE 你的表 ALTER COLUMN 列名 你的类型 null
declare cursor1 cursor for
select d.name from syscolumns a,sysobjects d where a.id=d.id and d.xtype='U' and a.name='code'
declare
@i varchar(1000)
open cursor1
fetch cursor1 into @i
while @@fetch_status=0
begin
exec('ALTER TABLE '+@i+' ALTER COLUMN code 你的类型 null')
fetch cursor1 into @i
end
close cursor1
deallocate cursor1