2009-10-22 12 views
10

J'ai déclaré les types suivants dans mon package PL/SQL:Utilisation des tableaux d'enregistrements dans PL/SQL

TYPE t_simple_object IS RECORD (
    wert NUMBER, 
    gs  NUMBER, 
    vl  NUMBER); 

TYPE t_obj_table IS TABLE OF t_simple_object 
    INDEX BY BINARY_INTEGER; 

Je Déclarez une variable:

obj t_obj_table; 

Cependant, quand je veux d'utiliser la variable, je ne peux pas initialiser ou l'étendre:

obj := t_obj_table(); 

donne la errror suivante:

PLS-00222: no function with name 'T_OBJ_TABLE' exists in this scope 

Si je n'initialise pas, je ne peux pas l'étendre à ajouter une date

obj.EXTEND(); 

donne une autre erreur:

PLS-00306: wrong number or types of arguments in call to 'EXTEND' 

Comment puis-je faire ce travail?

Répondre

19

Vous ne se prolongent pas une table indexée par « quelque chose », il vous suffit de l'utiliser ...

DECLARE 
    TYPE t_simple_object IS RECORD 
     (wert NUMBER 
     , gs  NUMBER 
     , vl  NUMBER 
    ); 

    TYPE t_obj_table IS TABLE OF t_simple_object 
    INDEX BY BINARY_INTEGER; 

    my_rec t_simple_object; 
    obj t_obj_table; 
BEGIN 
    my_rec.wert := 1; 
    my_rec.gs := 1; 
    my_rec.vl := 1; 
    obj(1) := my_rec; 
END; 
/

Pour utiliser la syntaxe PROLONGER, cet exemple devrait do it ...

DECLARE 
    TYPE t_simple_object IS RECORD 
     (wert NUMBER 
     , gs  NUMBER 
     , vl  NUMBER 
    ); 

    TYPE t_obj_table IS TABLE OF t_simple_object; 

    my_rec t_simple_object; 
    obj t_obj_table := t_obj_table(); 
BEGIN 
    obj.EXTEND; 
    my_rec.wert := 1; 
    my_rec.gs := 1; 
    my_rec.vl := 1; 
    obj(1) := my_rec; 
END; 
/

voir aussi this link (Ask Tom)

7

Vous ne pouvez pas étendre une matrice assosiative. Juste assigner des valeurs à elle

declare 
    TYPE t_simple_object IS RECORD (
    wert NUMBER, 
    gs  NUMBER, 
    vl  NUMBER); 

    TYPE t_obj_table IS TABLE OF t_simple_object INDEX BY BINARY_INTEGER; 

    simple_object t_simple_object; 
begin 
    simple_object.wert := 1; 
    simple_object.gs := 2; 
    simple_object.vl := 3; 
    obj(1) := simple_object; 
end; 
/
7

Si vous ne ne voulez pas utiliser un tableau associatif (table index-by), puis omettez la clause "INDEX BY BINARY_INTEGER". Votre code fonctionne alors OK:

declare 
    TYPE t_simple_object IS RECORD (
     wert NUMBER, 
     gs  NUMBER, 
     vl  NUMBER); 
    TYPE t_obj_table IS TABLE OF t_simple_object; 
    obj t_obj_table; 
begin 
    obj := t_obj_table(); 
    obj.EXTEND(); 
end; 
0

Ou tout simplement utiliser un enregistrement (ou un tableau associé de disques)

create or replace package p_test is 

    type t_rec is record (
    empname varchar2(50), 
    empaddr varchar2(50)); 

    function p_test_ret_record return t_rec; 

end p_test; 


create or replace package body p_test is 

    function p_test_ret_record return t_rec is 
    l_rec t_rec; 
    begin 
    l_rec.empname := 'P1'; 
    l_rec.empaddr := 'P2';  
    return l_rec; 
    end; 

end p_test; 

declare 
    -- Non-scalar parameters require additional processing 
    result p_test.t_rec; 
begin 
    -- Call the function 
    result := p_test.p_test_ret_record; 
    dbms_output.put_line('Name: ' || result.empname || ' Addr: ' || result.empaddr); 
end;