Delimiter //
/**
	Procédure qui supprime la table d'accréditation pré-calculé du membre
*/
Drop Procedure if Exists BUSINESS_COMPUTE_MEMBERS_CREDENTIALS_CLEAR_MEMBER_CRENDENTIALS//
 
Create Procedure BUSINESS_COMPUTE_MEMBERS_CREDENTIALS_CLEAR_MEMBER_CRENDENTIALS(
	In memberId Int Unsigned 
	, In credentialType Varchar(30)
)
Begin
    memberCredentialClear: begin
		Set @bcmcemc_credentialType = Coalesce(credentialType, 'ACCOUNTS');
# 		Set @bcmcemc_query = Concat('Drop Table if exists TMP_', @bcmcemc_credentialType, '_CREDENTIALS_MEMBER__', memberId);
# 		PREPARE credMemberTblDropStmt FROM @bcmcemc_query; 
# 		execute credMemberTblDropStmt 
# 			;
# 		Deallocate prepare credMemberTblDropStmt;
        Set @bcmcemc_mbrTableExists = Coalesce(
                (
                    Select count(*)
                    From information_schema.TABLES
                    Where
                            TABLE_SCHEMA = schema()
                      And `TABLE_NAME` = Concat('TMP_', @bcmcemc_credentialType, '_CREDENTIALS_MEMBER__', memberId)
                )
            , 0
            );
        if @bcmcemc_mbrTableExists > 0 Then
            Set @bcmcemc_memberId = memberId;
            Set @bcmcemc_query = Concat('Delete tar From TMP_', @bcmcemc_credentialType, '_CREDENTIALS_MEMBER__', memberId, ' tar'
                )
            ;
            PREPARE credMemberDelStmt FROM @bcmcemc_query;
            execute credMemberDelStmt
            ;
            Deallocate prepare credMemberDelStmt;
        End if;
    end ;

	memberCredentialAllClear: Begin
		Set @bcmcemc_mbrAlltableExists = Coalesce(
									(
										Select count(*)
										From information_schema.TABLES
										Where
										TABLE_SCHEMA = schema()
										And `TABLE_NAME` = Concat('TMP_', @bcmcemc_credentialType, '_CREDENTIALS_MEMBER__ALL')
									)
									, 0
								);
		if @bcmcemc_mbrAlltableExists > 0 Then
			Set @bcmcemc_memberId = memberId;
			Set @bcmcemc_query = Concat('Delete tar From TMP_', @bcmcemc_credentialType, '_CREDENTIALS_MEMBER__ALL tar'
							, ' Where MEMBERS_ID_MEMBERS = ?'
						)
					;
			PREPARE credMemberAllDelStmt FROM @bcmcemc_query; 
			execute credMemberAllDelStmt 
					using @bcmcemc_memberId 
				;
			Deallocate prepare credMemberAllDelStmt;
		End if;

	end;
end //
Delimiter ;

