请教如何判断info表中的khName字段是否存在,如果不存在就新建该字段。如: info表中的  khName 为 nvarchar(50)类型,默认值为: 无

解决方案 »

  1.   

    if not exists(select 1 from syscolumns a inner join sysobjects b on a.id=b.id where b.type='U' and b.name='info' and a.name='khName'
    alter table info add khName nvarchar(50)
      

  2.   

    create table info(id int)
    go
    if not exists(select 1 from syscolumns a inner join sysobjects b on a.id=b.id where b.type='U' and b.name='info' and a.name='khName')
    alter table info add khName nvarchar(50)
    go
    select * from info
    /*
    id          khName
    ----------- --------------------------------------------------(0 行受影响)*/
    go
    drop table info
      

  3.   

    我发现sql版的回贴速度太快了,非常感谢中。