CREATE OR REPLACE FUNCTION getMaterialLocation(p_classification varchar, p_materialtypeid int, p_materialphysicaltypeid int, p_libraryunitid int, p_exemplarystatusid int, p_knowledgeareaid varchar)
RETURNS TABLE (materiallocationid int) AS
/*************************************************************************************
  NAME: getMaterialLocation
  DESCRIPTION: Obtém a localização de material.
**************************************************************************************/
$BODY$ 
BEGIN
    RETURN QUERY SELECT gtcmateriallocation.materiallocationid
                   FROM gtcmateriallocation
                  WHERE gtcmateriallocation.materiallocationid IN (SELECT getMaterialLocationClassification
                                                                     FROM getMaterialLocationClassification(p_classification))
                    AND gtcmateriallocation.materiallocationid IN (SELECT getMaterialLocationMaterialType 
                                                                     FROM getMaterialLocationMaterialType(p_materialtypeid))
                    AND gtcmateriallocation.materiallocationid IN (SELECT getMaterialLocationMaterialPhysicalType 
                                                                     FROM getMaterialLocationMaterialPhysicalType(p_materialphysicaltypeid))
                    AND gtcmateriallocation.materiallocationid IN (SELECT getMaterialLocationLibraryUnitId 
                                                                     FROM getMaterialLocationLibraryUnitId(p_libraryunitid))
                    AND gtcmateriallocation.materiallocationid IN (SELECT getMaterialLocationExemplaryStatus 
                                                                     FROM getMaterialLocationExemplaryStatus(p_exemplarystatusid))
                    AND gtcmateriallocation.materiallocationid IN (SELECT getMaterialLocationKnowledgeArea 
                                                                     FROM getMaterialLocationKnowledgeArea(p_knowledgeareaid))
             ORDER BY gtcmateriallocation.materiallocationid LIMIT 1;
          
END; 
$BODY$ 
language plpgsql;
