日期:2014-05-18  浏览次数:20426 次

通过脚本添加字段不成功
/*create table test(
  lxh int,
  msg varchar(80)
)
*/

if not exists (select name from syscolumns where id = object_id('test') and name = 'ss') 
begin
  alter table test add ss int default(0);
  update test set ss = lxh;
end;
执行脚本后报错,报错信息如下:
消息 207,级别 16,状态 1,第 0 行
列名 'ss' 无效。
另:if not exists (select name from syscolumns where id = object_id('test') and name = 'ss') 
begin
  alter table test add ss int default(0);
  --update test set ss = test.lxh;
end;
执行这句脚本是成功的

------解决方案--------------------
SQL code

if not exists (select name from syscolumns where id = object_id('test') and name = 'ss')  
begin
  alter table test add ss int default(0);
  exec('update test set ss = lxh;')
end;