Drop existing FK constraint and create a new constraint. In the new constraint, use a function check.

Function

CREATE FUNCTION [dbo].[fnChecktbTable01]
  (@Table01Id bigint)
RETURNS bit
AS
BEGIN
  IF EXISTS(SELECT Table01Id FROM tbTable01 WITH (NOLOCK) WHERE Table01Id = @Table01Id)
    RETURN 1
  ELSE IF EXISTS(SELECT Table01Id FROM [LS_ARCHIVE].[DB_01].[dbo].[tbTable01] WITH (NOLOCK) WHERE Table01Id = @Table01Id)
    RETURN 1
  RETURN 0
END
GO

Constraint

ALTER TABLE [dbo].[tbTable_02]  WITH CHECK ADD  CONSTRAINT [CHECK_tbTable_02_tbTable01] CHECK (([dbo].[fnChecktbTable01]([biSendTransId])=(1)))
GO

ALTER TABLE [dbo].[tbTable_02] CHECK CONSTRAINT [CHECK_tbTable_02_tbTable01]
GO
Last modified: June 30, 2021

Author

Comments

Write a Reply or Comment