当前位置: 代码迷 >> Sql Server >> 通过脚本添加字段不成功解决思路
  详细解决方案

通过脚本添加字段不成功解决思路

热度:71   发布时间:2016-04-27 13:10:43.0
通过脚本添加字段不成功
/*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;
  相关解决方案