TSQL在另一个数据库中删除FK

时间:2017-08-22 11:08:47

标签: sql-server tsql foreign-keys sql-server-2014

MSSQL 2014

在database1中工作时是否可以在数据库2中删除FK?

这对我不起作用

use database1
go

    IF EXISTS (SELECT 1 FROM database2.sys.foreign_keys WHERE object_id = OBJECT_ID(N'fk_in_tbl_in_other_db') AND parent_object_id = OBJECT_ID(N'database2.dbo.tableName'))
    BEGIN
        ALTER TABLE database2.dbo.tableName DROP CONSTRAINT fk_in_tbl_in_other_db;
    END

1 个答案:

答案 0 :(得分:0)

好的,我已经设法找到了解决方案,只是对WHERE子句的一个小修改

替换

WHERE name = N'fk_in_tbl_in_other_db'


IF EXISTS (SELECT 1 FROM database2.sys.foreign_keys WHERE name = N'fk_in_tbl_in_other_db' AND parent_object_id = OBJECT_ID(N'database2.dbo.tableName'))
BEGIN
    ALTER TABLE database2.dbo.tableName DROP CONSTRAINT fk_in_tbl_in_other_db;
END

.mybutton{
    font: bold 15px Segoe UI, Helvetica Neue, Arial;
}