sqloracle-databaseplsqlsql-types

Assign ref variable value oracle


hHey, I want to set the value of the assignment value in the method of the node_ty. However I get the error that navigation through Ref variables is not allowed or the typ I want to assign is not correct. So I don't really know how to do that. Could you help me with that?

CREATE OR REPLACE TYPE property_ty AS OBJECT(
name VARCHAR2(100)
);
/

CREATE OR REPLACE TYPE assignment_ty AS OBJECT(
value VARCHAR2(100),
property REF property_ty
);
/

CREATE OR REPLACE TYPE property_tty AS TABLE OF property_ty;
/


CREATE OR REPLACE TYPE node_ty AS OBJECT(
x NUMBER,
y NUMBER,
assignment REF assignment_ty,
property property_tty,
MEMBER PROCEDURE set_assignment (name VARCHAR2, value VARCHAR2)
);
/
CREATE OR REPLACE TYPE BODY node_ty AS
    MEMBER PROCEDURE set_assignment (name VARCHAR2, value VARCHAR2) AS
    prop_not_added EXCEPTION;
    prop_exists BOOLEAN := FALSE;
    assig_test assignment_ty;
    prop property_ty;
    BEGIN
        FOR i IN 1..self.property.COUNT
        LOOP
            IF property(i).name = name THEN
               prop.name :=name;
               assig_test.value :=value;
               assig_test.property := prop;
               assignment := assig_test;
               prop_exists := TRUE;

            END IF;
        END LOOP;

        IF prop_exists = FALSE THEN
            RAISE prop_not_added;
        END IF;

        EXCEPTION
            WHEN prop_not_added THEN
            DBMS_OUTPUT.PUT_LINE('Property cannot be set');
    END;
END;
/

Solution

  • You have to create the necessary setup and then query the data from the setup.I am giving only the setup which is different from yours.The remaining you can keep it as same as your setup

       create table t00 of property_ty;
       insert into t00 values('san');
    
       create table t0 of assignment_ty;
       insert into t0 select 'xxx',REF(p) from t00 p;
    
       CREATE OR REPLACE TYPE node_ty AS OBJECT(
        x NUMBER,
        y NUMBER,
        assignment REF assignment_ty,
        property property_tty,
        MEMBER PROCEDURE set_assignment (p_name VARCHAR2, p_value VARCHAR2)
        );
    
       create or replace TYPE BODY node_ty AS
    MEMBER PROCEDURE set_assignment (p_name  VARCHAR2, p_value  VARCHAR2) AS
    prop_not_added EXCEPTION;
    prop_exists BOOLEAN := FALSE;
    assig_test REF  assignment_ty;
    prop1 property_ty;
    prop REF property_ty;
    BEGIN
    
        FOR i IN 1..self.property.COUNT
        LOOP
            IF property(i).name = p_name THEN
    
             SELECT REF(a) INTO assignment FROM t0 a,t00 b where a.value=p_value and b.name=p_name
             and ref(b)=property;
    
               prop_exists := TRUE;
    
            END IF;
        END LOOP;
    
        IF prop_exists = FALSE THEN
            RAISE prop_not_added;
        END IF;
    
        EXCEPTION
            WHEN prop_not_added THEN
            DBMS_OUTPUT.PUT_LINE('Property cannot be set');
    END;
    END;