CREATE PROCEDURE  MIG_MBC_NEW_PARENTS_Sp
AS
BEGIN
	
	DELETE FROM MIG_MBC_NEW_PARENTS

	INSERT INTO MIG_MBC_NEW_PARENTS
	(
		[moid],
		[rule_id],
		[ikey],
		[revi]
	)
	SELECT 
		MBC_RULES.MBC_ID,
		MIG_MBC_RULES.rule_id,
		PARENT_ITEM_KEY.CLASS_KEY AS PARENT_ITEM_KEY,
		PARENT_ITEM_REVISION.CLASS_KEY AS PARENT_ITEM_REV
	FROM MBC_RULES
	JOIN MIG_MBC_RULES 
	ON MIG_MBC_RULES.moid = MBC_RULES.MBC_ID 
	AND MIG_MBC_RULES.dmrd = MBC_RULES.RULE_ID
	LEFT JOIN RULE_AFFC_OBJS PARENT
	ON PARENT.MBC_ID = MBC_RULES.MBC_ID
	AND PARENT.POSITION_NUMBER = MBC_RULES.RULE_ID
	LEFT JOIN OBJECTS_KEYS PARENT_ITEM_KEY
	  ON PARENT_ITEM_KEY.OBJECT_ID = PARENT.OBJECT_ID
	 AND PARENT_ITEM_KEY.ORDER_NO = '1'
	LEFT JOIN OBJECTS_KEYS PARENT_ITEM_REVISION
	  ON PARENT_ITEM_REVISION.OBJECT_ID = PARENT.OBJECT_ID
	 AND PARENT_ITEM_REVISION.ORDER_NO = '2'
	WHERE MBC_RULES.ACTION_TYPE = 0

END

