Sql SSDT在发布期间禁用还原约束

Sql SSDT在发布期间禁用还原约束,sql,sql-server,constraints,sql-server-data-tools,Sql,Sql Server,Constraints,Sql Server Data Tools,我正在向SSDT项目添加脚本,以管理静态数据表中数据的添加/更新。其中一些表相互引用,因此我需要能够在更新过程中关闭约束,以便让数据处于不一致的状态,直到所有脚本都运行为止。我为表a创建了一个文件,为表B创建了一个单独的文件 一旦完成,我需要重新启用所有约束,并让系统检查数据以确保其一致性。需要注意的是,由于各种原因,一些约束当前被禁用,我需要确保它们恢复到脚本之前的状态 我似乎找不到列出FK约束当前状态的视图/表 有没有办法让SSDT publish自动执行此操作?如果没有,谁能推荐一个好的解

我正在向SSDT项目添加脚本,以管理静态数据表中数据的添加/更新。其中一些表相互引用,因此我需要能够在更新过程中关闭约束,以便让数据处于不一致的状态,直到所有脚本都运行为止。我为表a创建了一个文件,为表B创建了一个单独的文件

一旦完成,我需要重新启用所有约束,并让系统检查数据以确保其一致性。需要注意的是,由于各种原因,一些约束当前被禁用,我需要确保它们恢复到脚本之前的状态

我似乎找不到列出FK约束当前状态的视图/表


有没有办法让SSDT publish自动执行此操作?如果没有,谁能推荐一个好的解决方案呢?

最后,我使用了sys.Foreign\u键和sys.check\u约束的组合,因为disable all命令禁用了这两个键。我在执行工作之前记录约束的状态,然后在最后将它们重新启用到原始状态

If OBJECT_ID('tempdb..#tempConstraints') is not null Drop Table #tempConstraints;
GO
IF (SELECT OBJECT_ID('tempdb..#tmpScriptErrors')) IS NOT NULL DROP TABLE #tmpScriptErrors
GO
CREATE TABLE #tmpScriptErrors (Error int)
GO
Create Table #tempConstraints
(
    ConstraintName nVarchar(200),
    TableName nVarchar(200),
    SchemaName nVarchar(200),
    IsNotTrusted bit
);
GO

Begin Tran

Insert into #tempConstraints (ConstraintName, TableName, SchemaName, IsNotTrusted)
Select K.name, object_name(K.parent_object_id), SCHEMA_NAME(T.schema_id), K.Is_Not_Trusted
FROM sys.foreign_keys K 
    Inner Join sys.tables T on K.parent_object_id = T.object_id
Where is_disabled = 0
Union all
Select K.name, object_name(K.parent_object_id), SCHEMA_NAME(T.schema_id), K.Is_Not_Trusted
from sys.check_constraints K
    Inner Join sys.tables T on K.parent_object_id = T.object_id
Where is_disabled = 0


--Disable the Constraints.
Print 'Disabling Constraints'
Exec sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL';

----------------------------------------------------------------------------------------
--Do Work Here
----------------------------------------------------------------------------------------

Declare @name nvarchar(200);
Declare @table nvarchar(200);
Declare @schema nvarchar(200);
Declare @script nvarchar(max);
Declare @NotTrusted bit;

Declare constraints_cursor CURSOR FOR 
    Select ConstraintName, TableName, SchemaName, IsNotTrusted
    From #tempConstraints;

Open constraints_cursor;

Fetch Next from constraints_cursor
into @name, @table, @schema, @NotTrusted;

While @@FETCH_STATUS = 0
Begin
    --Restore each of the Constraints back to exactly the state they were in prior to disabling.

    If @NotTrusted = 1
        Set @script = 'ALTER TABLE [' + @schema + '].[' + @table + '] WITH NOCHECK CHECK CONSTRAINT [' + @name + ']';
    Else
        Set @script = 'ALTER TABLE [' + @schema + '].[' + @table + '] WITH CHECK CHECK CONSTRAINT [' + @name + ']';


    exec sp_executesql @script;
    If @@ERROR <> 0
    Begin
        INSERT  INTO #tmpScriptErrors (Error)
        VALUES (1);
    End

    Fetch Next from constraints_cursor
    into @name, @table, @schema, @NotTrusted;
End
Close constraints_cursor;
Deallocate constraints_cursor;

If exists (Select 'x' from #tmpScriptErrors)
    ROLLBACK TRAN;
Else
    COMMIT TRAN;

Drop table #tmpScriptErrors
GO
Drop table #tempConstraints
GO

您可能希望查看sys.foreign_键,以查看约束是否已启用。然后,您可以在表/变量中设置该值,以了解随后要适当设置的状态。我可能仍然会在部署后脚本中运行这些脚本-可能是一个脚本,以避免出现带有临时表或表变量的问题。谢谢Peter。这就解决了-我已经成功地记录了启用的详细信息和我想要恢复到与以前完全相同的状态的受信任状态-缺点等等。我将发布脚本以供将来参考。