How to update subtype attribute values in oracle database?

675 views Asked by At

How to update column value of a subtype?

Code like this can't access the grade attribute of student:

update persons set grade = 'graduated';
CREATE OR REPLACE TYPE person (
    name varchar2(20),
    age number,
    address varchar2(20)
) NOT FINAL;

CREATE OR REPLACE TYPE student UNDER person (
    grade varchar2(20)
) NOT FINAL;

CREATE TABLE persons OF person;

INSERT INTO persons VALUES (student('Jon', 'undergraduate'));

How I change student grade to 'graduated'?

2

There are 2 answers

0
Guilherme On BEST ANSWER

I found a answer to my problem.
This is the code to update subtype attributes:

DECLARE
    s student;

BEGIN
    select treat(value(p) as student) into s from persons p where name = 'Jon';
    s.grade = 'graduated';
    update persons p set value(p) = s where name = 'Jon';
END;
2
hotfix On

that should work

update persons s set VALUE(s) = student(treat(value(s) as student).name, 'graduated');