if exists(select 1 from sysobjects where name='B') 
drop table b
select  100 b  into b 
alter table b add id int  IDENTITY (1, 1) NOT NULL 
select b.* from b  
go
select id from b 

解决方案 »

  1.   


    exec('
    if exists(select 1 from sysobjects where name=''B'') drop table b
    select  100 b  into b 
    alter table b add id int  IDENTITY (1, 1) NOT NULL 
    ')
    exec ('
    select b.* from b  
    select id from b --不报错了吧?!
    ')
      

  2.   

    加go就相当于分两次执行,可以解决在查询分析器执行的问题,但是如果在程序里或者存储过程里,就不能用go了
      

  3.   

    8992026:
    谢谢!
    为什么用exec ('alter table b add id int  IDENTITY (1, 1) NOT NULL 
    ') 可以 而 exec 'alter table b add id int  IDENTITY (1, 1) NOT NULL 
    '可以
      

  4.   

    为什么用exec ('alter table b add id int  IDENTITY (1, 1) NOT NULL 
    ') 不可以 而 exec 'alter table b add id int  IDENTITY (1, 1) NOT NULL 
    '可以
      

  5.   

    哎,反了
    为什么用exec ('alter table b add id int  IDENTITY (1, 1) NOT NULL 
    ') 可以 而 exec 'alter table b add id int  IDENTITY (1, 1) NOT NULL 
    '不可以