用动态语句
if exists (select * from dbo.sysobjects where id = object_id(N'abc') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table abccreate table abc(num int)select *
from abcinsert 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 abcexec('
ALTER Table abc add edf numeric(18,2),hij numeric(18,2)
')select *
from abcexec('
update abc
set edf = 5
where num = 4update abc
set hij = 6
where edf = 5
')select *
from abc

解决方案 »

  1.   

    ALTER Table abc add edf numeric(18,2),hij numeric(18,2)
    go加个go
      

  2.   

    create table abc(num int)select *
    from abcinsert 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 abcALTER Table abc add edf numeric(18,2),hij numeric(18,2)
    goselect *
    from abcupdate abc
    set edf = 5
    where num = 4update abc
    set hij = 6
    where edf = 5select *
    from abcdrop 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 行)*/