
/* Code Fragment 4.16 INCUMBENTS.PCN is a current foreign key for POSITIONS.PCN (both tables are temporal). */

CREATE TRIGGER Current_Referential_Integrity
ON INCUMBENTS
FOR INSERT, UPDATE, DELETE
AS
BEGIN
	IF (EXISTS (	SELECT *
			FROM INCUMBENTS AS I
			WHERE I.END_DATE = '3000-01-01'
			AND NOT EXISTS (SELECT *
					FROM POSITIONS AS P
					WHERE I.PCN = P.PCN
					AND P.END_DATE = '3000-01-01')))
	RAISERROR('Transaction violates current referential integrity',1,2)
	ROLLBACK TRANSACTION
END
