无法在 SQL Server 2005 中删除约束,“无法删除约束。请参阅以前的错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2592977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:56:37  来源:igfitidea点击:

Unable to drop constraint in SQL server 2005, "Could not drop constraint. See previous errors"

sqlsql-serverconstraintsalter-table

提问by DannykPowell

I'm trying to drop a constraint on a DB table, something like:

我正在尝试删除数据库表上的约束,例如:

ALTER TABLE MyTable drop CONSTRAINT FK_MyTable_AnotherTable

But the execution just runs and runs. If I stop it I see:

但执行只是运行和运行。如果我停止它,我会看到:

Msg 3727, Level 16, State 0, Line 2
Could not drop constraint. See previous errors.

Web search throws up various pages but note that the constraint is properly named and I am trying to remove it using the correct name

网络搜索会抛出各种页面,但请注意约束已正确命名,我正在尝试使用正确的名称将其删除

采纳答案by DannykPowell

Found a way to sort this, although I don't understand why it was necessary.

找到了一种对此进行排序的方法,尽管我不明白为什么需要这样做。

Have been able to drop the constraint by disabling it first:

已经能够通过首先禁用它来删除约束:

ALTER MyTable NOCHECK CONSTRAINT FK_MyTable_AnotherTable

The drop then completes fine

然后下降完成

Would still welcome any comments on the reason why this is necessary

仍然欢迎任何评论为什么这是必要的

回答by Zeeshan Ajmal

I was having the same issue on SQL Server 2008 R2, I solved my problem with below line hopefully it will work for someone else as well :)

我在SQL Server 2008 R2上遇到了同样的问题,我用下面的代码解决了我的问题,希望它也适用于其他人:)

    Alter Table [Table Name]
    DROP Column [Column Name]

回答by Andomar

Verify that you've not already dropped the constraint, like:

验证您尚未删除约束,例如:

SELECT OBJECT_ID('FK_MyTable_AnotherTable')

If this returns null, your constraint no longer exists. That would explain the error message.

如果返回null,则您的约束不再存在。这将解释错误消息。

回答by flam3

I had same problem.

我有同样的问题。

The reason was that I made a mistake in my cursor statement, which was iterating some constraint I was to drop. So this error occurred when the constraint was actually removed in the same transaction. Then I did a rollback and checked if it existed: it did (of course, after rollback!).

原因是我在游标语句中犯了一个错误,它迭代了我要删除的一些约束。所以这个错误发生在约束在同一个事务中被实际删除的时候。然后我做了一个回滚并检查它是否存在:它确实存在(当然,在回滚之后!)。

Check if it really exists at the moment of dropping.

在掉落的那一刻检查它是否真的存在。