if exists (select * from dbo.sysobjects where id = object_id(N'abc') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table abc create table abc(num int) select * from abc insert into abc(num) values(1) insert into abc(num) values(2) insert into abc(num) values(3) insert into abc(num) values(4) insert into abc(num) values(5) select * from abc ALTER Table abc add edf numeric(18,2),hij numeric(18,2) select * from abc update abc set edf = 5 where num = 4 update abc set hij = 6 where edf = 5 select * from abc
if exists (select * from dbo.sysobjects where id = object_id(N'abc') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table abc create table abc(num int) select * from abc insert into abc(num) values(1) insert into abc(num) values(2) insert into abc(num) values(3) insert into abc(num) values(4) insert into abc(num) values(5) select * from abc ALTER Table abc add edf numeric(18,2),hij numeric(18,2) GO select * from abc update abc set edf = 5 where num = 4 update abc set hij = 6 where edf = 5 select * from abc
用动态语句 if exists (select * from dbo.sysobjects where id = object_id(N'abc') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table abc create table abc(num int) select * from abc insert into abc(num) values(1) insert into abc(num) values(2) insert into abc(num) values(3) insert into abc(num) values(4) insert into abc(num) values(5) select * from abc exec(' ALTER Table abc add edf numeric(18,2),hij numeric(18,2) ') select * from abc exec(' update abc set edf = 5 where num = 4 update abc set hij = 6 where edf = 5 ') select * from abc
create table abc(num int) select * from abc insert into abc(num) values(1) insert into abc(num) values(2) insert into abc(num) values(3) insert into abc(num) values(4) insert into abc(num) values(5) select * from abc ALTER Table abc add edf numeric(18,2),hij numeric(18,2) go select * from abc update abc set edf = 5 where num = 4 update abc set hij = 6 where edf = 5 select * from abc drop table abc /* num ----------- 1 2 3 4 5 (所影响的行数为 5 行) num edf hij ----------- -------------------- -------------------- 1 NULL NULL 2 NULL NULL 3 NULL NULL 4 NULL NULL 5 NULL NULL (所影响的行数为 5 行) (所影响的行数为 1 行) (所影响的行数为 1 行) num edf hij ----------- -------------------- -------------------- 1 NULL NULL 2 NULL NULL 3 NULL NULL 4 5.00 6.00 5 NULL NULL (所影响的行数为 5 行) */