有表test
想在表中建列 p1,p2,p3...p16
请问怎么做。最好脚本。谢谢

解决方案 »

  1.   

    declare @I int
    set @i = 1
    while @i < 17
    begin
        exec('alter table test add p'+ltrim(@i)+' char(100)')
        set @I = @i +1
    end
    go
      

  2.   

    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
      

  3.   

    create table ta(id int )
    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
    ----------- ------------ -------------------------- ------------------- ---------------------------------- --------------------------------- ----------------------- --------------------------------------------------- --------------- ----------------------------------------------------------------- --------------- ------------------------------------------- ---------------------- ------------------ -------------------- ------------------------------ ---------------------
    */
      

  4.   


    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 
      

  5.   


    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