我正在尝试编写一个过程来更快地从任何表中删除唯一约束。
IF EXISTS
(SELECT *
FROM dbo.sysobjects
WHERE id = object_id(N'[dba].[spu_drop_uq_index]'))
DROP PROCEDURE [dba].[spu_drop_uq_index]
GO
CREATE PROCEDURE [dba].[spu_drop_uq_index] (@table varchar(1000), @index varchar(1000))
AS
BEGIN
DECLARE @sql varchar(1000)
SET @sql = 'ALTER TABLE ['+@table+'] DROP CONSTRAINT ['+@index+']'
IF EXISTS (SELECT name FROM sysindexes WHERE name = @index)
EXEC @sql
END
GO
EXEC [dba].[spu_drop_uq_index] @table = 'aaa', @index = 'UQ_xxx'
GO
但我收到一个错误:
The name 'ALTER TABLE [aaa] DROP CONSTRAINT [UQ_xxx]' is not a valid identifier.
但是,如果我不是动态执行它,它会成功:
ALTER TABLE [aaa] DROP CONSTRAINT [UQ_xxx]
我究竟做错了什么?:) 谢谢!