sql >> Base de Datos >  >> RDS >> Oracle

¿Cómo usar un tipo de tabla en una declaración SELECT FROM?

En SQL, solo puede usar el tipo de tabla que se define en el nivel de esquema (no en el nivel de paquete o procedimiento), y la tabla indexada (matriz asociativa) no se puede definir en el nivel de esquema. Entonces, debe definir una tabla anidada como esta

create type exch_row as object (
    currency_cd VARCHAR2(9),
    exch_rt_eur NUMBER,
    exch_rt_usd NUMBER);

create type exch_tbl as table of exch_row;

Y luego puede usarlo en SQL con el operador TABLE, por ejemplo:

declare
   l_row     exch_row;
   exch_rt   exch_tbl;
begin
   l_row := exch_row('PLN', 100, 100);
   exch_rt  := exch_tbl(l_row);

   for r in (select i.*
               from item i, TABLE(exch_rt) rt
              where i.currency = rt.currency_cd) loop
      -- your code here
   end loop;
end;
/