求循环建列脚本 有表test想在表中建列 p1,p2,p3...p16请问怎么做。最好脚本。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 declare @I intset @i = 1while @i < 17begin exec('alter table test add p'+ltrim(@i)+' char(100)') set @I = @i +1endgo declare @I int ,@S VARCHAR(800)set @i = 1 while @i < 17 begin SET @S='alter table a add p'+cast(@i as varchar(10))+' char(100)' exec(@s) set @I = @i +1 end go create table ta(id int )godeclare @I int ,@s varchar(100)set @i = 1 while @i < 17 begin set @s =('alter table ta add p'+ltrim(@i)+' char(100)') exec (@s) set @I = @i +1 end goselect * from tadrop table ta/*id p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16----------- ------------ -------------------------- ------------------- ---------------------------------- --------------------------------- ----------------------- --------------------------------------------------- --------------- ----------------------------------------------------------------- --------------- ------------------------------------------- ---------------------- ------------------ -------------------- ------------------------------ ---------------------*/ declare @I int ,@S VARCHAR(800)set @i = 1 while @i <= 3 begin SET @S='alter table a add p'+convert(char,@i)+' varchar(100)' exec(@s) set @I = @i +1 end create table T(id int)--declare @i intset @i=1while @i<=16 begin exec('alter table T add p'+@i+' int') set @i=@i+1 end 请教一句update语句的写法? 如何实现动态指定select top 的数量?********************* 求教哪里能找到sql server 2005的学习资料? C#连sqlesrver2000问题 SQL问题 关于外键的关系问题,求助! .bak在SQL server2000下,还原数据库报错出错 关于数据库复制出现的问题 我想要点在SQL SERVER里写存储过程的文档 请教老鸟高人一条SQL语句的写法 求一条SQL语句 如何实现定时对服务器上的一个库存表做一下记录
set @i = 1
while @i < 17
begin
exec('alter table test add p'+ltrim(@i)+' char(100)')
set @I = @i +1
end
go
set @i = 1
while @i < 17
begin
SET @S='alter table a add p'+cast(@i as varchar(10))+' char(100)' exec(@s)
set @I = @i +1
end
go
go
declare @I int ,@s varchar(100)
set @i = 1
while @i < 17
begin
set @s =('alter table ta add p'+ltrim(@i)+' char(100)')
exec (@s)
set @I = @i +1
end
goselect * from tadrop table ta/*
id p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16
----------- ------------ -------------------------- ------------------- ---------------------------------- --------------------------------- ----------------------- --------------------------------------------------- --------------- ----------------------------------------------------------------- --------------- ------------------------------------------- ---------------------- ------------------ -------------------- ------------------------------ ---------------------
*/
declare @I int ,@S VARCHAR(800)
set @i = 1
while @i <= 3
begin
SET @S='alter table a add p'+convert(char,@i)+' varchar(100)' exec(@s)
set @I = @i +1
end
create table T(id int)
--
declare @i int
set @i=1
while @i<=16
begin
exec('alter table T add p'+@i+' int')
set @i=@i+1
end