我正在嘗試編寫一個過程以更快地從任何表中刪除唯一約束。刪除唯一約束的過程
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 ['[email protected]+'] DROP CONSTRAINT ['[email protected]+']'
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]
我在做什麼錯? :) 謝謝!
我太親近了!非常感謝您指出這一點! :)) – 2012-07-27 10:43:00
@AndriusNaruševičius另外,使用quotename而不是手動添加[] – podiluska 2012-07-27 10:44:36