首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-04-26 
通过脚本添加字段不成功/*create table test(lxh int,msg varchar(80))*/if not exists (select name from

通过脚本添加字段不成功
/*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; 

热点排行