/**
 * Retorna string dos segmentos do cardápio base
 * 
 * @param {Number} P_NRCARDAPIOBASE Chave do cardápio base
 * revisada
 */
CREATE OR REPLACE FUNCTION SEGMENTO_CARDBASE(P_NRCARDAPIOBASE IN NUMBER) RETURN VARCHAR2
AS
   V_SEGMENTOS VARCHAR2(1000);
BEGIN
   V_SEGMENTOS := '';
   FOR C_SEGS IN
   (SELECT S.DSSEGMER
      FROM SEGMERCARDBASE SC
           INNER JOIN SEGMERCADO S ON S.CDSEGMER = SC.CDSEGMER
     WHERE SC.NRCARDAPIOBASE = P_NRCARDAPIOBASE
     ORDER BY S.DSSEGMER) LOOP
      --Separador de valores
      IF LENGTH(V_SEGMENTOS) > 0 THEN
         V_SEGMENTOS := V_SEGMENTOS || ', ';
      END IF;
      -- Terminador caso limite tenha sido atingido
      IF LENGTH(V_SEGMENTOS) + LENGTH(C_SEGS.DSSEGMER) > 1000 THEN         
         V_SEGMENTOS := V_SEGMENTOS || SUBSTR(C_SEGS.DSSEGMER, 1, 995) || '(...)';
      ELSE
         V_SEGMENTOS := V_SEGMENTOS || C_SEGS.DSSEGMER;
      END IF;
   END LOOP;
   RETURN V_SEGMENTOS;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('SEGMENTO_CARDBASE','6.35.009'); END;
/
