CREATE OR REPLACE PROCEDURE DELETA_CARDTRAB
(
	P_NRCARDAPIOTRAB NUMBER
)
AS
BEGIN
	-- Receitas
	DELETE FROM RECEPRATCARDTRAB
	 WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB;

	-- Produtos
	DELETE FROM PRODUTOCARDTRAB
	 WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB;
	-- Dias 
	FOR C_DIAS IN
	(
		SELECT NRDIACARDTRAB
		  FROM DIACARDTRAB
		 WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB		 
	) LOOP
		-- Configuracao
		FOR C_CNF IN
		(
			SELECT NRCONFIGCARDTRAB
			  FROM CONFIGCARDTRAB
			 WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB			 
		) LOOP
			-- Composicao
			FOR C_COMP IN
			(
				SELECT NRCOMPCARDTRAB
				  FROM COMPCARDTRAB
				 WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB
				   AND NRDIACARDTRAB = C_DIAS.NRDIACARDTRAB
				   AND NRCONFIGCARDTRAB = C_CNF.NRCONFIGCARDTRAB
			) LOOP
				DELETE FROM COMPCARDTRAB
				 WHERE NRCOMPCARDTRAB = C_COMP.NRCOMPCARDTRAB;
			END LOOP;
			
		END LOOP;		
	END LOOP;

	DELETE FROM DIACARDTRAB
	 WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB;

	DELETE FROM CONFIGCARDTRAB
	  WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB;

	DELETE FROM CARDAPIOTRAB	
	 WHERE NRCARDAPIOTRAB = P_NRCARDAPIOTRAB;
END;
/