create table a  a1 varchar(20)然后添加一例. alter table a  add a2 varchar(20)
再去判断是否本身A表里是否存在C例如果有就把a2更新为C的内容
if exists(select 1 from syscolumns where [id] in(select [id] from sysobjects where name='a') and [name]='c')
begin update a set a2=c 此时if判断为否应该不执行update 但会报错服务器: 消息 207,级别 16,状态 3,行 1
列名 'c无效。
不明白这其中原因..望达人指点.

解决方案 »

  1.   

    if exists(select 1 from syscolumns where [id] in(select [id] from sysobjects where name='a') and [name]='c')
    begin EXEC('update a set a2=c ')
      

  2.   

    SQL SERVER 是先编译生成执行计划, 再执行的
    而编译的时候, 如果 C 列不存在, 则当然没办法生成执行计划, 当然会报错的而把 UPDATE 放在 EXEC 中的话, 里面的语句是执行时编译的, 所以不会出问题
      

  3.   

    是语法错误,编译不能过去,因为找不到‘c’列,
    if exists(select 1 from syscolumns where [id] in(select [id] from sysobjects where name='a') and [name]='c')
    EXEC('update a set a2=c ')
    end
    就不会报错。