Using operator deref - manipulating objects in pl sql, PL-SQL Programming

Using Operator DEREF:

You cannot navigate through refs within the PL/SQL procedural statements. Rather than, you should use the operator DEREF in the SQL statement. The DEREF take as its argument that reference to an object, and then returns the value of that object. If the ref is dangling, the DEREF returns a null object.

In the illustration below, you dereference the ref to a Person object. Note that you select the ref from dummy table dual. You do not require specifying an object table and search criteria as each object stored in an object table has an immutable, exclusive object identifier that is a part of every ref to that object.

DECLARE

p1 Person;

p_ref REF Person;

name VARCHAR2(15);

BEGIN

...

/* Suppose that p_ref holds a valid reference

to an object stored in an object table. */

SELECT DEREF(p_ref) INTO p1 FROM dual;

name := p1.last_name;

You can use DEREF in successive SQL statements to dereference refs, as the

following example shows:

CREATE TYPE PersonRef AS OBJECT (p_ref REF Person)

/

DECLARE

name VARCHAR2(15);

pr_ref REF PersonRef;

pr PersonRef;

p Person;

BEGIN

...

/* Assume pr_ref holds a valid reference. */

SELECT DEREF(pr_ref) INTO pr FROM dual;

SELECT DEREF(pr.p_ref) INTO p FROM dual;

name := p.last_name;

...

END

/

The later illustration shows that you cannot use the operator DEREF within procedural statements:

BEGIN

...

p1 := DEREF(p_ref); -- illegal

Within the SQL statements, you can use the dot notation to navigate throughout the object columns to ref attributes and through one ref attribute to the other. You can also navigate through the ref columns to attributes if you use a table alias. For illustration, the syntax below is valid:

table_alias.object_column.ref_attribute

table_alias.object_column.ref_attribute.attribute

table_alias.ref_column.attribute

Suppose that you have to run the SQL*Plus script below that creates object types Address and Person and object table persons:

CREATE TYPE Address AS OBJECT (

street VARCHAR2(35),

city VARCHAR2(15),

state CHAR(2),

zip_code INTEGER)

/

CREATE TYPE Person AS OBJECT (

first_name VARCHAR2(15),

last_name VARCHAR2(15),

birthday DATE,

home_address REF Address, -- shared with other Person objects

phone_number VARCHAR2(15))

/

CREATE TABLE persons OF Person

/

The Ref attribute home_address corresponds to a column in the object table persons that holds refs to the Address objects stored in some another table. After populating the tables, you can select a particular address by de-referencing its ref, as shown:

DECLARE

addr1 Address,

addr2 Address,

...

BEGIN

SELECT DEREF(home_address) INTO addr1 FROM persons p

WHERE p.last_name = 'Derringer';

In the illustration below, you navigate through ref column home_address to attribute the street. In this situation, the table alias is needed.

DECLARE

my_street VARCHAR2(25),

...

BEGIN

SELECT p.home_address.street INTO my_street FROM persons p

WHERE p.last_name = 'Lucas';

Posted Date: 10/6/2012 8:22:34 AM | Location : United States







Related Discussions:- Using operator deref - manipulating objects in pl sql, Assignment Help, Ask Question on Using operator deref - manipulating objects in pl sql, Get Answer, Expert's Help, Using operator deref - manipulating objects in pl sql Discussions

Write discussion on Using operator deref - manipulating objects in pl sql
Your posts are moderated
Related Questions
Keyword & Parameter Description: WHEN: This keyword introduces the exception handler. You can have many exceptions execute the similar sequence of the statements by follo

Expression: This is a randomly complex combination of constants, variables, literals, operators, & function calls. The simplest expression consists of a single variable. If th

Great Plains (Microsoft Dynamics) Purchases Report Project Description: I want to build a purchases report that matches the General Ledger. presently, when I join the PM20

Assigning and Comparing Collections One collection can be assigned to other by an SELECT, INSERT, UPDATE, or FETCH statement, an assignment statement, or by a subprogram call. A

Cause of Indeterminacy in SQL One root cause of indeterminacy in SQL lies in its implementation of comparison for equality. For certain system-defined types it is possible for

Ending Transactions A good quality programming practice is to commit or roll back every transaction explicitly. Whether you rollback or issue the commit in your PL/SQL program

Variable Declaration - SQL SQL's support for variables is very similar to Tutorial D's, except that the syntax for creating persistent  variables-base tables-is quite differen

Using the BULK COLLECT Clause The keywords BULK COLLECT specify the SQL engine to bulk-bind output collections before returning them to the PL/SQL engine. You can use these ke

Package Body: The package specification is implemented by the package body. That is, the package body has the definition of every cursor and the subprogram declared in the pac

Majority of Differences among 9i, 10G, 11G :- These are some combine feature which has differences among others. Automatic Workload Repository (AWR) Drop database' s