你如何强制重命名???
表'dbo.x的重命名失败。 (Microsoft.SqlServer.Smo)
执行Transact-SQL语句或批处理时发生异常。 (Microsoft.SqlServer.ConnectionInfo)
对象'[dbo]。[x]'无法重命名,因为该对象参与强制依赖。 (Microsoft SQL Server,错误:15336)
答案 0 :(得分:45)
找到“强制执行的依赖项”,然后删除或禁用它们。
通过“强制依赖”,它意味着Schema绑定,因此您必须专门查看它。
这是一个查询对象的模式绑定引用的查询:
select o.name as ObjName, r.name as ReferencedObj
from sys.sql_dependencies d
join sys.objects o on o.object_id=d.object_id
join sys.objects r on r.object_id=d.referenced_major_id
where d.class=1
AND r.name = @YourObjectName
正如我在评论中所指出的那样,强制使用 no 方式来覆盖Schema Binding。当您使用Schema Binding时,您明确地说“Do not 让我或其他任何人覆盖它。” Schema Binding的唯一方法是撤消它,这是故意的。
答案 1 :(得分:4)
我有同样的问题,我的问题是我有一个COMPUTED FIELD使用我试图重命名的列。
通过从所选答案中运行查询,我能够判断出已经强制执行的依赖项,但我无法确切地看到问题是什么
答案 2 :(得分:3)
试试这个:
/*
Example 1: Rename a table dbo.MyTable -> dbo.YourTable
EXEC dbo.USP_DROP_ENFORCED_DEPENDENCIES @SchemaName=N'dbo', @EntityName=N'MyTable', @Debug=1;
EXEC sp_rename N'dbo.MyTable', N'YourTable', N'OBJECT'
Example 2: Rename a column dbo.MyTable.MyColumn -> dbo.MyTable.YourColumn
EXEC dbo.USP_DROP_ENFORCED_DEPENDENCIES @SchemaName=N'dbo', @EntityName=N'MyTable', @ColumnName=N'MyColumn' @Debug=1;
EXEC sp_rename N'dbo.MyTable.MyColumn', N'YourColumn', N'COLUMN'
*/
CREATE Procedure dbo.USP_DROP_ENFORCED_DEPENDENCIES
(
@SchemaName sysname = 'dbo',
@EntityName sysname,
@ColumnName sysname = NULL,
@Debug bit = 0
)
AS
BEGIN
SET NOCOUNT ON;
SET ROWCOUNT 0;
DECLARE @ReferencingEntitySchema sysname, @ReferencingEntityName sysname, @ReferencingEntityType nvarchar(8), @SqlScript nvarchar(512);
DECLARE ReferencingEntitiesCursor CURSOR LOCAL FORWARD_ONLY
FOR
SELECT OBJECT_SCHEMA_NAME(dep.referencing_id) AS [schema]
,referencing_entity.name
,CASE referencing_entity.type
WHEN 'V' THEN N'VIEW'
ELSE /*IF, FN, TF*/ N'FUNCTION'
END as [type]
FROM sys.sql_expression_dependencies AS dep
INNER JOIN sys.objects AS referencing_entity
ON dep.referencing_id = referencing_entity.object_id
WHERE dep.referenced_entity_name = @EntityName
AND dep.referenced_schema_name = @SchemaName
AND is_schema_bound_reference = 1
AND ((@ColumnName IS NULL AND dep.referenced_minor_id = 0) OR COL_NAME(dep.referenced_id, dep.referenced_minor_id) = @ColumnName)
OPEN ReferencingEntitiesCursor
FETCH NEXT FROM ReferencingEntitiesCursor
INTO @ReferencingEntitySchema, @ReferencingEntityName, @ReferencingEntityType;
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC dbo.USP_DROP_ENFORCED_DEPENDENCIES @SchemaName=@ReferencingEntitySchema, @EntityName=@ReferencingEntityName, @Debug=@Debug;
--The goal is to produce the following script:
/*
DROP FUNCTION dbo.UFN_SOME_FUNCTION;
OR
DROP VIEW dbo.UFN_SOME_VIEW;
*/
SET @SqlScript = N'DROP ' + @ReferencingEntityType + N' ' + @ReferencingEntitySchema + '.' + @ReferencingEntityName;
IF(@Debug = 1)
RAISERROR (@SqlScript, 0/*severity*/, 0/*state*/) WITH NOWAIT;
EXEC (@SqlScript);
FETCH NEXT FROM ReferencingEntitiesCursor
INTO @ReferencingEntitySchema, @ReferencingEntityName, @ReferencingEntityType;
END
CLOSE ReferencingEntitiesCursor;
DEALLOCATE ReferencingEntitiesCursor;
END
GO
答案 3 :(得分:1)
在SQL Server对象浏览器中,右键单击包含问题的表,然后选择View Dependencies
。接下来在列出的视图中,右键单击(视图)并在New SQL Query Editor窗口中选择SCRIPT to CREATE VIEW,然后从CREATE VIEW t-sql脚本中删除WITH SCHEMABINDING
并运行修订后的CREATE VIEW t-sql。这会将模式依赖关系从表中取消链接。我能够在此时重新创建表格(DROP,RENAME等)。
注意:
模式绑定可以在数据库中的函数和其他对象上进行。 在抛出错误的对象上使用
View Dependencies
是必不可少的 解决问题。顺便说一句:
我最初添加了模式绑定以启用视图索引。保持一个 基础表上的良好索引可以减轻性能损失 在视图中没有一个。
答案 4 :(得分:0)
我有这样的问题。我删除了对此DB对象的约束,重命名了DB对象,然后重新创建了这些约束。这解决了我的问题。