2017-08-22 62 views
0

MSSQL 2014TSQL在另一個數據庫中刪除FK

在數據庫1中工作時,是否可以在數據庫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 

回答

0

好吧,我設法找到一個解決方案,只是一個小的修改WHERE子句

更換

WHERE object_id = OBJECT_ID(N'fk_in_tbl_in_other_db') 

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