09.01.2015 Views

PL/SQL User's Guide and Reference

PL/SQL User's Guide and Reference

PL/SQL User's Guide and Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Manipulating Objects<br />

table_alias.object_column.ref_attribute.attribute<br />

table_alias.ref_column.attribute<br />

Assume that you have run the following <strong>SQL</strong>*Plus script, which creates object types<br />

Address <strong>and</strong> Person <strong>and</strong> object table persons:<br />

CREATE TYPE Address AS OBJECT (<br />

street VARCHAR2(35),<br />

city VARCHAR2(15),<br />

state CHAR(2),<br />

zip_code INTEGER)<br />

/<br />

CREATE TYPE Person AS OBJECT (<br />

first_name VARCHAR2(15),<br />

last_name VARCHAR2(15),<br />

birthday DATE,<br />

home_address REF Address, -- shared with other Person objects<br />

phone_number VARCHAR2(15))<br />

/<br />

CREATE TABLE persons OF Person<br />

/<br />

Ref attribute home_address corresponds to a column in object table persons that<br />

holds refs to Address objects stored in some other table. After populating the<br />

tables, you can select a particular address by dereferencing its ref, as follows:<br />

DECLARE<br />

addr1 Address,<br />

addr2 Address,<br />

...<br />

BEGIN<br />

SELECT DEREF(home_address) INTO addr1 FROM persons p<br />

WHERE p.last_name = ’Derringer’;<br />

In the example below, you navigate through ref column home_address to attribute<br />

street. In this case, a table alias is required.<br />

DECLARE<br />

my_street VARCHAR2(25),<br />

...<br />

BEGIN<br />

SELECT p.home_address.street INTO my_street FROM persons p<br />

WHERE p.last_name = ’Lucas’;<br />

10-38 <strong>PL</strong>/<strong>SQL</strong> User’s <strong>Guide</strong> <strong>and</strong> <strong>Reference</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!