我要删除一个表的主键declare @cons varchar(100)
select @cons = name from sysobjects where xtype = 'pk' and parent_obj = object_id('table_1')
alter table [dbo.table_1] drop constraint @cons写完后执行 报错
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '@cons'.请帮忙看看哪里错了好吧 谢之

解决方案 »

  1.   

    exec('alter table [dbo.table_1] drop constraint '+@cons )
      

  2.   

    declare @cons varchar(100)
    select @cons = name from sysobjects where xtype = 'pk' and parent_obj = object_id('table_1')
    exec('alter table [table_1] drop constraint '+@cons+'')
      

  3.   

    [dbo.table_1]也不对, 应该为 dbo.[table_1]
      

  4.   


    declare @cons varchar(100) 
    select @cons = name from sysobjects where xtype = 'pk' and parent_obj = object_id('table_1') 
    alter table dbo.[table_1] drop constraint @cons