怎么将alter和select语句写在一个脚本里面。
比如用alter add一个,然后select语句对add进行查询。
sql server中

解决方案 »

  1.   

    alter table t add name varchar(20)
    select name from t
      

  2.   

    查詢時用動態如alter table t1 add num intexec('select num from t1')或用
    select * from t1--不指定列名就行了
      

  3.   

    动态sql
    declare @sql varchar(max)
    set @sql='alter table t1 add [column] varchar(100)'
    exec(@sql)
    set @sql='select [column] from t1 '
    exec(@sql)
      

  4.   

    if object_id('tb') is not null
     drop table tbCreate table tb
    (
    id int
    )
    Go
    if exists(select * from syscolumns where id  = object_id('tb') and name ='name')
    begin
    select * from tb
    end
    else
    begin
    alter table tb add name varchar(20)
    select * from tb
    end
    Go