如何初始化记录类型并克服ORA-06530:引用未初始化的复合错误

时间:2018-03-02 14:03:14

标签: sql oracle plsql variable-assignment runtimeexception

我想将值初始化为RECORD TYPE。以下是我的代码。

我在运行时遇到"ORA-06530: Reference to uninitialized composite error"。代码编译得很好。

CREATE OR REPLACE TYPE TY_PERSONAL_MASTER AS OBJECT
(
ID      NUMBER(20),
FNAME   VARCHAR2(30),
LNAME   VARCHAR2(30)
);


CREATE OR REPLACE TYPE TY_PROF_MASTERS IS TABLE OF TY_PROF_MASTER;

CREATE OR REPLACE TYPE TY_PROF_MASTER AS OBJECT 
(
ID        NUMBER(20),
EMPLOYER  VARCHAR2(20),
DOJ       DATE
);

CREATE TABLE TABLE_HOBBIES
(
ID      NUMBER(20),
HNAME   VARCHAR2(20)
);


CREATE OR REPLACE PACKAGE INITIALIZE IS

TYPE PERSON IS RECORD(
PERSONAL        TY_PERSONAL_MASTER,
PROFESSIONAL    TY_PROF_MASTERS,
v_hobbies       TABLE_HOBBIES%ROWTYE
)

END INITIALIZE;

CREATE OR REPLACE PACKAGE BODY INITIALIZE IS

FUNCTION FN_INITIALIZE(P_ACTION IN VARCHAR2) RETURN BOOLEAN IS
L_TO_INITIALIZE PERSON;

BEGIN
L_TO_INITIALIZE.PERSONAL.ID := 1;
-- The above line throws "ORA-06530: Reference to uninitialized composite error" at runtime.
-- I have just given example of assigning 1 value. However, I want to initialize all the values. 

END FN_INITIALIZE;
END INITIALIZE;

1 个答案:

答案 0 :(得分:2)

它们是不同的类型,所以你必须以不同的方式处理它们:

CREATE OR REPLACE PACKAGE BODY INITIALIZE IS

  FUNCTION FN_INITIALIZE(P_ACTION IN VARCHAR2) RETURN BOOLEAN IS
    L_TO_INITIALIZE PERSON;
  BEGIN
    -- object so need to call constructor
    L_TO_INITIALIZE.PERSONAL := TY_PERSONAL_MASTER(1, 'Bob', 'Hope');
    -- can also assign values directly once created
    L_TO_INITIALIZE.PERSONAL.ID := 1;
    L_TO_INITIALIZE.PERSONAL.FNAME := 'Bob';
    L_TO_INITIALIZE.PERSONAL.LNAME := 'Hope';

    -- table type so need to call constructor
    L_TO_INITIALIZE.PROFESSIONAL := new TY_PROF_MASTERS();
    -- need to extend to make room for new entry
    L_TO_INITIALIZE.PROFESSIONAL.extend(1);
    -- object so need to call constructor
    L_TO_INITIALIZE.PROFESSIONAL(1) := new TY_PROF_MASTER(2, 'USO', date '1941-05-06');
    -- can also assign values directly once created
    L_TO_INITIALIZE.PROFESSIONAL(1).DOJ := date '1941-03-01';

    -- record so no constructor, just assign values
    L_TO_INITIALIZE.v_hobbies.ID := 3;
    L_TO_INITIALIZE.v_hobbies.HNAME := 'Fishing';

    RETURN TRUE;
  END FN_INITIALIZE;
END INITIALIZE;
/