读书人

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

发布时间: 2012-04-26 14:01:31 作者: rapoo

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

读书人网 >SQL Server

热点推荐