-- EXEC MIG_CONDITIONS_Sp
CREATE OR ALTER PROCEDURE MIG_CONDITIONS_Sp 
AS 
BEGIN
	TRUNCATE TABLE MIG_CONDITIONS
	--Mask Conditions
	INSERT INTO MIG_CONDITIONS 
	SELECT	MIG_ZZID_TABLE_GUID.NEW_GUID,
			MIG_MASK_CONDITIONS.cnum,
			MIG_MASK_CONDITIONS.anor,
			MIG_MASK_CONDITIONS.open_br,
			MIG_MASK_CONDITIONS.keyn,
			MIG_MASK_CONDITIONS.oper,
			MIG_MASK_CONDITIONS.valu,
			MIG_MASK_CONDITIONS.clos_br,
			MIG_MASK_CONDITIONS.updt
	FROM	MIG_ZZID_TABLE_GUID, 
			MIG_MASKS, 
			ZZ_MASK,
			MIG_MASK_CONDITIONS
	WHERE	MIG_ZZID_TABLE_GUID.TABLE_NAME = 'ZZ_MASK'
	AND		MIG_MASKS.mask = ZZ_MASK.MASK_ID
	AND		MIG_MASKS.mask = MIG_MASK_CONDITIONS.mask
	AND		MIG_ZZID_TABLE_GUID.ZZ_ID = ZZ_MASK.ZZ_ID

	
	--Sync Rules/Conditions
	INSERT INTO MIG_CONDITIONS 
	SELECT	MIG_ZZID_TABLE_GUID.NEW_GUID,
			MIG_SYNC_RULES.cnum,
			MIG_SYNC_RULES.anor,
			MIG_SYNC_RULES.open_br,
			MIG_SYNC_RULES.keyn,
			MIG_SYNC_RULES.oper,
			MIG_SYNC_RULES.valu,
			MIG_SYNC_RULES.clos_br,
			MIG_SYNC_RULES.updt
	FROM	MIG_ZZID_TABLE_GUID, 
			MIG_SITE,
			MIG_SYNC_RULES
	WHERE	MIG_ZZID_TABLE_GUID.TABLE_NAME = 'SITE'
	AND		MIG_SITE.SITE_KEY = MIG_SYNC_RULES.site_key
	AND		MIG_ZZID_TABLE_GUID.ZZ_ID = MIG_SITE.ZZ_ID

	
	--Security Rules/Conditions
	INSERT INTO MIG_CONDITIONS 
	SELECT	MIG_ZZID_TABLE_GUID.NEW_GUID,
			MIG_SEC_RULES_CONDITIONS.cnum,
			MIG_SEC_RULES_CONDITIONS.anor,
			MIG_SEC_RULES_CONDITIONS.open_br,
			MIG_SEC_RULES_CONDITIONS.keyn,
			MIG_SEC_RULES_CONDITIONS.oper,
			MIG_SEC_RULES_CONDITIONS.valu,
			MIG_SEC_RULES_CONDITIONS.clos_br,
			MIG_SEC_RULES_CONDITIONS.updt
	FROM	MIG_ZZID_TABLE_GUID, 
			SEC_RULES,
			MIG_SEC_RULES_CONDITIONS
	WHERE	MIG_ZZID_TABLE_GUID.TABLE_NAME = 'SEC_RULES'
	AND		MIG_SEC_RULES_CONDITIONS.rule_id = SEC_RULES.RULE_ID
	AND		MIG_ZZID_TABLE_GUID.ZZ_ID = SEC_RULES.ZZ_ID
END


