	DROP TEMPORARY TABLE IF EXISTS beneficiaireage_temp;	 

	CREATE TEMPORARY TABLE beneficiaireage_temp
	SELECT 
		A.* 
	FROM beneficiaire A 
	INNER JOIN adherent B ON ((A.codeSociete=B.codeSociete) AND  (B.id=A.idAdherent) AND (B.idPolice=A.idPolice))
	INNER JOIN police C ON (C.id=A.idPolice)
	INNER JOIN clients t ON ((A.codeSociete=t.codeSociete) AND  (t.id=C.idClient))	
	INNER JOIN gc_assureur Gc ON (Gc.codeGcAssureur=C.codeGcAssureur)	
	WHERE (A.idPolice>0) AND A.nomBeneficiaire NOT LIKE "%AVENANT%"
	AND (A.dateEntree BETWEEN p_debut AND p_fin )
	AND (CASE WHEN p_garant IS NULL THEN p.codeGcAssureur IS NOT NULL ELSE p.codeGcAssureur=p_garant END)  
	AND (CASE WHEN p_idPolice IS NULL THEN A.idPolice IS NOT NULL ELSE A.idPolice=p_idPolice END);


	DELETE FROM beneficiaireage_temp WHERE (dateSortie<p_debut) AND (dateSortie>"0000-00-00"); 
	
	
	
	JOIN beneficiaireage_temp t ON (A.id=t.id)