Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask PL-SQL Expert

Opening a Cursor Variable

The OPEN-FOR statement relates a cursor variable with the multi-row query, executes the query, and then identifies the result set. The syntax for opening a cursor is as shown below:

OPEN {cursor_variable_name | :host_cursor_variable_name}

FOR select_statement;

Where the host_cursor_variable_name identify the cursor variable declared in the PL/SQL host environments like an OCI or Pro C program.

Dissimilar cursors, the cursor variables take no parameters. Though, no flexibility is lost as you can pass entire queries (not just parameters) to the cursor variable. The query can reference the host variables and the PL/SQL parameters, functions, and variables but cannot be FOR UPDATE. In the illustration below, you open the cursor variable emp_cv. Note that you can apply the cursor attributes (%ISOPEN, %FOUND, %NOTFOUND, and %ROWCOUNT) to the cursor variable.

IF NOT emp_cv%ISOPEN THEN

/* Open cursor variable. */

OPEN emp_cv FOR SELECT * FROM emp;

END IF;

The Other OPEN-FOR statements can open similar cursor variable for various queries. You do not require closing a cursor variable before reopening it.  Whenever you reopen a cursor variable for various queries, the earlier query is lost.

Usually, you open the cursor variable by passing it to the stored procedure which declares a cursor variable as one of its formal parameters. For illustration, the packaged procedure below opens the cursor variable emp_cv:

CREATE PACKAGE emp_data AS

...

TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE;

PROCEDURE open_emp_cv (emp_cv IN OUT EmpCurTyp);

END emp_data;

CREATE PACKAGE BODY emp_data AS

...

PROCEDURE open_emp_cv (emp_cv IN OUT EmpCurTyp) IS

BEGIN

OPEN emp_cv FOR SELECT * FROM emp;

END open_emp_cv;

END emp_data;

Whenever you declare a cursor variable as the formal parameter of a subprogram which opens the cursor variable, you should specify the IN OUT mode. In the similar way, the subprogram can pass an open cursor back to the caller.

Or else, you can use a stand-alone process to open the cursor variable. Basically define the REF CURSOR type in the separate package, and then reference that type in the stand-alone process. For illustration, if you create the following bodiless package, you can make stand-alone process that references the types it defines:

CREATE PACKAGE cv_types AS

TYPE GenericCurTyp IS REF CURSOR;

TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE;

TYPE DeptCurTyp IS REF CURSOR RETURN dept%ROWTYPE;

...

END cv_types;

In the next illustration, you create a stand-alone process which references the REF CURSOR type EmpCurTyp that is defined in the package cv_types:

CREATE PROCEDURE open_emp_cv (emp_cv IN OUT cv_types.EmpCurTyp) AS

BEGIN

OPEN emp_cv FOR SELECT * FROM emp;

END open_emp_cv;

To integrate the data retrieval, you can group the type-compatible queries in a stored procedure. In the illustration below, the packaged procedure declare a selector as one of its formal parameters. (In this framework, the selector is a variable used to select one of few alternatives in a conditional control statement.) Whenever called, the procedure opens the cursor variable emp_cv for the chosen query.

CREATE PACKAGE emp_data AS

TYPE GenericCurTyp IS REF CURSOR;

TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE;

PROCEDURE open_emp_cv (emp_cv IN OUT EmpCurTyp, choice NUMBER);

END emp_data;

CREATE PACKAGE BODY emp_data AS

PROCEDURE open_emp_cv (

emp_cv IN OUT EmpCurTyp,

choice NUMBER) IS

BEGIN

IF choice = 1 THEN

OPEN emp_cv FOR SELECT * FROM emp WHERE comm IS NOT NULL;

ELSIF choice = 2 THEN

OPEN emp_cv FOR SELECT * FROM emp WHERE sal > 2500;

ELSIF choice = 3 THEN

OPEN emp_cv FOR SELECT * FROM emp WHERE deptno = 20;

END IF;

END open_emp_cv;

END emp_data;

For additional flexibility, you can pass a cursor variable & a selector to the stored procedure which executes queries with various return types. Consider this illustration as shown:

CREATE PACKAGE BODY emp_data AS

PROCEDURE open_cv (

generic_cv IN OUT GenericCurTyp,

choice NUMBER) IS

BEGIN

IF choice = 1 THEN

OPEN generic_cv FOR SELECT * FROM emp;

ELSIF choice = 2 THEN

OPEN generic_cv FOR SELECT * FROM dept;

ELSIF choice = 3 THEN

OPEN generic_cv FOR SELECT * FROM salgrade;

END IF;

END open_cv;

END emp_data;

PL-SQL, Programming

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

Have any Question?


Related Questions in PL-SQL

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 ...

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 ...

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 ...

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 ...

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 ...

  • 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