Oracle/PL/SQL/Tipos compuestos

De Wikilibros, la colección de libros de texto de contenido libre.
< Oracle‎ | PL/SQL

Registros[editar]

El usuario puede definir sus propios registros tambien su tipos de datos, incluyendo tipos independientes, derivados de tablas y otros registros:

declare
  type t_registro is record (
    guia varchar2(20) not null := 'Registros y Colecciones',
    apellido employees.last_name%type,
    region regins%rowtype
  );  
  v_registro t_registro;
begin
  v_registro.apellido := 'Gonzalez';
  v_registro.guia := 'Registros';
  v_registro.region.region_id := 5;
  v_registro.region.region_name := 'Antártida';
end;

Colecciones[editar]

Nested Tables[editar]

declare
  type t_vector is table of varchar2(30);
  v_vector t_vector;
begin
  v_vector := t_vector('Uno', 'Dos', 'Tres');

  for i in v_vector.first .. v_vector.last loop
    dbms_output.put_line(i ||': '|| v_vector(i));
  end loop;
end;

Associative Arrays[editar]

Se asocia un valor con un subíndice, sin un orden ni capacidad determinados.

Ejemplo con subíndice entero:

declare
  type t_vector is table of varchar2(30) index by pls_integer;
  v_vector t_vector;
  n pls_integer;
begin
  v_vector(10) := 'Diez';
  v_vector(20) := 'Veinte';
  v_vector(12) := 'Doce';

  for i in v_vector.first .. v_vector.last loop
    if (v_vector.exists(i)) then
      dbms_output.put_line(i ||': '|| v_vector(i));
    end if;
  end loop;

  dbms_output.put_line('count: '|| v_vector.count);

  n := v_vector.first;
  while n is not NULL loop
    dbms_output.put_line(n ||': '|| v_vector(n));
    n := v_vector.next(n);
  end loop;
end;

Ejemplo con subíndice de texto:

declare
  type t_vector is table of number(3) index by varchar2(10);
  v_vector t_vector;
  subind varchar2(10);
begin
  v_vector('Cuarenta') := 40;
  v_vector('cuarenta') := -40;
  v_vector('Cinco') := 5;
  v_vector('Quince') := 15;

  dbms_output.put_line('count: '|| v_vector.count);

  subind := v_vector.first;
  while subind is not NULL loop
    dbms_output.put_line(subind ||': '|| v_vector(subind));
    subind := v_vector.next(subind);
  end loop;
end;

Operaciones masivas[editar]

Lectura de Colecciones usando SELECT INTO y BULK COLLECT.

Ejemplo con Nested Table:

declare
  type t_emp is table of employees%rowtype;
  v_emp t_emp;
begin
  select *
  bulk collect into v_emp
  from employees
  where salary > 12000;

  for i in v_emp.first .. v_emp.last loop
    dbms_output.put_line(v_emp(i).last_name ||': '|| v_emp(i).salary);
  end loop;
end;

Ejemplo con Associative Arrays indexados por enteros:

declare
  type t_emp is table of employees%rowtype index by pls_integer;
  v_emp t_emp;
begin
  select *
  bulk collect into v_emp
  from employees
  where salary > 12000;

  for i in v_emp.first .. v_emp.last loop
    dbms_output.put_line(v_emp(i).last_name ||': '|| v_emp(i).salary);
  end loop;
end;

Optimización de DML:

declare
  type t_vector is table of ...;
  v_vector t_vector;
begin
  forall i in v_vector.first .. v_vector.last
    insert into tabla values (v_vector(i));

  forall i in v_vector.first .. v_vector.last
    delete from tabla where campo = v_vector(i);
end;

También es válido para UPDATE.