我目前正在研究Oracle11g,我已经掌握了对象表。 经典例子:
create or replace TYPE T_person AS OBJECT
(
name varchar2(20),
surname varchar2(20),
MEMBER PROCEDURE Set_Sir
);
/
create or replace TYPE BODY T_person AS
MEMBER PROCEDURE Set_Sir IS
BEGIN
name := 'Sir ' || name;
END;
END;
/
CREATE TABLE person_obj_table OF t_person;
insert into person_obj_table values (t_person('John', 'Scofield'));
现在我想调用“Set_Sir”为所有名为'John'的人添加前缀'Sir'(在这种情况下只有一个)。我怎么能这样做?
update 7/1/2018: 这是迄今为止发现的最佳解决方案(仅适用于一个元素):
declare
p_pers t_person;
begin
select t_person(name, surname) into p_pers
from PERSON_OBJ_TABLE
where NAME = 'John';
p_pers.set_sir();
update PERSON_OBJ_TABLE p
set p = p_pers
where name = 'John';
end;
答案 0 :(得分:0)
似乎应该有更好的方法来做到这一点,但我能想到的最好的方法是在SQL和PL / SQL之间切换。
declare
v_rowid rowid;
begin
for people in
(
select treat(value(p) as t_person) a_person, rowid the_rowid
from person_obj_table p
) loop
people.a_person.set_sir;
update person_obj_table p
set p = people.a_person
where rowid = people.the_rowid;
end loop;
end;
/