2010-10-18 48 views
7

¿Es posible usar SELECT FROM cuando se utiliza una matriz asociativa? Estoy pasando una matriz a un procedimiento almacenado a través de una aplicación .NET, y quiero poder usar esa matriz como una condición cuando seleccione desde otra tabla. Digamos que estoy pasando una matriz de ID con el procedimiento, quiero ser capaz de hacer esto:Seleccionar de PLSQL Matriz asociativa?

select * from table1 where userID in (select column_value from array)

El tipo para la matriz se define en el paquete:

type id_array is type of number index by pls_integer

Respuesta

1

No, no puede seleccionar matrices PL/SQL, ya que usa SQL en las sentencias select from, aunque puede usar tipos de tablas anidadas definidas por DB en SQL. This short article puede ayudarlo a comenzar.

Tome un vistazo a este sencillo exmple sintética:

> create type temp_t as table of int;/ 
Type created. 
> select 'test' from dual where 1 in (select * from table(temp_t(1,2,3))); 

'TES 
---- 
test 
+0

Así que no hay manera de acceder a una matriz asociativa en absoluto? ¿Se usa solo para pasar datos? – ashtame

+0

¿es posible crear una matriz asociativa como una tabla? – ashtame

+0

@ashtame no es posible usar una matriz asociativa en SQL, solo se permiten tablas y varrays anidados. – andr

10

Sí, es posible, envolviendo la matriz mediante una función pipeline. He aquí una buena imprimación sobre funciones pipeline:

http://www.oracle-developer.net/display.php?id=429

UPDATE: Oracle 12c ahora soporta la consulta de matrices asociativas usando el operador TABLE, siempre que el tipo se declara en una especificación del paquete: https://galobalda.wordpress.com/2014/08/02/new-in-oracle-12c-querying-an-associative-array-in-plsql-programs/

por ejemplo

select * from table1 
where userID in (select column_value from table(array)); 
-1

Un ejemplo utilizando PLSQL (para seleccionar de una tabla anidada):

create type temp_r as OBJECT(
    temp_varchar2 varchar2(100), 
    temp_number number(20) 
    ); 
/

create type temp_t as TABLE of temp_r; 
/ 

set serveroutput on size 1000000 
/

-- PLSQL starts here 
declare 
    temp_rec temp_r := temp_r(null, null); -- empty constructor to initialize object 
    temp_table temp_t := temp_t();   -- empty constructor to initialize object 
    lv_ref_cursor  SYS_REFCURSOR; 

    lv_temp_varchar2 varchar(100); 
    lv_temp_number number(20); 

begin 
    temp_rec.temp_varchar2 := 'first'; 
    temp_rec.temp_number := 1; 

    temp_table.extend; 
    temp_table(1) := temp_rec; 
    temp_table.extend; 
    temp_table(2) := temp_r('second', 2); 


    OPEN lv_ref_cursor FOR 
     SELECT temp_varchar2, temp_number 
     FROM table(temp_table) 
     where temp_number = 1; 

    fetch lv_ref_cursor into lv_temp_varchar2, lv_temp_number; 
    close lv_ref_cursor; 

    dbms_output.put_line('returns: ' || lv_temp_varchar2 || ', ' || lv_temp_number); 

end; 
/