
/* Code Fragment 4.20 INCUMBENTS.PCN is a current foreign key for POSITIONS.PCN (only POSITIONS is temporal). */

CREATE TRIGGER Current_Referential_Integrity
ON INCUMBENTS
FOR INSERT, UPDATE, DELETE
AS
BEGIN
	IF (EXISTS (	SELECT *
			FROM INCUMBENTS AS I
			WHERE 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
