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
             INNER JOIN (SELECT getMaterialLocationClassification(p_classification)) CLASSIF
                     ON (gtcmateriallocation.materiallocationid = CLASSIF.getMaterialLocationClassification)
             INNER JOIN (SELECT getMaterialLocationMaterialType(p_materialtypeid)) MATTYPE
                     ON (gtcmateriallocation.materiallocationid = MATTYPE.getMaterialLocationMaterialType)
             INNER JOIN (SELECT getMaterialLocationMaterialPhysicalType(p_materialphysicaltypeid)) MATPHYSICALTYPE
                     ON (gtcmateriallocation.materiallocationid = MATPHYSICALTYPE.getMaterialLocationMaterialPhysicalType)
             INNER JOIN (SELECT getMaterialLocationLibraryUnitId(p_libraryunitid)) LIBRARY
                     ON (gtcmateriallocation.materiallocationid = LIBRARY.getMaterialLocationLibraryUnitId)
             INNER JOIN (SELECT getMaterialLocationExemplaryStatus(p_exemplarystatusid)) EXEMPLARYSTATUS
                     ON (gtcmateriallocation.materiallocationid = EXEMPLARYSTATUS.getMaterialLocationExemplaryStatus)
             INNER JOIN (SELECT getMaterialLocationKnowledgeArea(p_knowledgeareaid)) KNOWLEDGEAREA
                     ON (gtcmateriallocation.materiallocationid = KNOWLEDGEAREA.getMaterialLocationKnowledgeArea)
             ORDER BY gtcmateriallocation.materiallocationid LIMIT 1;    
          
END; 
$BODY$ 
language plpgsql;
