quinta-feira, 25 de março de 2010

PL/SQL - Como obter os dados de um dicionário ordenados

Se quizerem obter os dados de um dicionário como o seguinte, basta que tenha o índice único, que ao fazer "instância do dicionário".FIRST irão obter o record mais "baixo".

TYPE a_places_i_lived IS TABLE OF VARCHAR2(50) 
INDEX BY VARCHAR2(10);

No link seguinte têm um exemplo completo.

DECLARE
-- Declare the table
   TYPE a_places_i_lived IS TABLE OF VARCHAR2 (50)
      INDEX BY VARCHAR2 (10);

--declare my table variable
   v_places_i_lived   a_places_i_lived;
-- Declare a temporary holding variable
   v_current          VARCHAR2 (10);
BEGIN
-- Initialize the array in unsorted order
   v_places_i_lived ('LA') := 'New Orleans';
   v_places_i_lived ('FL') := 'Tampa';
   v_places_i_lived ('TX') := 'San Antonio';
   v_places_i_lived ('CA') := 'Los Angeles';
   v_places_i_lived ('CT') := 'New Haven';

--starts from the end
   v_current := v_places_i_lived.LAST;

   LOOP
    -- Exit when we hit the bottom of the table
      EXIT WHEN v_current IS NULL;
    -- Print out
      DBMS_OUTPUT.put_line ('Index is: ' || v_current||' Value is: ' || v_places_i_lived (v_current));

-- delete the current
      v_places_i_lived.delete(v_current);
      v_current := v_places_i_lived.LAST;
   END LOOP;
END;

Sem comentários:

Enviar um comentário

Seguidores

Contribuidores