Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask PL-SQL Expert

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';

PL-SQL, Programming

  • Category:- PL-SQL
  • Reference No.:- M9510573

Have any Question?


Related Questions in PL-SQL

Purpose of the assessment with ulo mapping the purpose of

Purpose of the assessment (with ULO Mapping) The purpose of this assignment is to develop skills in managing data in databases and to gain understanding of data model development and implementation using a commercially a ...

Assignment - queries functions and triggersaimthe aims of

Assignment - Queries, Functions and Triggers Aim The aims of this assignment are to: formulate SQL queries; populate an RDBMS with a real dataset, and analyse the data; design test data for testing SQL queries; create SQ ...

Continuing the project you have worked on in weeks 1-4 in

Continuing the project you have worked on in Weeks 1-4, in this final week, complete the following tasks: Refine your database and SQL statements by incorporating your instructor's feedback. Verify that the database comp ...

Complete the following tasksin microsoft access create the

Complete the following tasks: In Microsoft Access, create the database and tables that you identified in W3 Assignment 2. In Microsoft Word, write the SQL statements to create the database and tables. Write SQL statement ...

For this assignment you will be provided a database backup

For this assignment, you will be provided a database backup for a database called FinanceDB. You will have to restore this backup to your own version of SQL Server. All of the questions in this assignment relate to the F ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As