Reader small image

You're reading from  Advanced Oracle PL/SQL Developer's Guide (Second Edition) - Second Edition

Product typeBook
Published inFeb 2016
Reading LevelIntermediate
Publisher
ISBN-139781785284809
Edition2nd Edition
Languages
Right arrow
Author (1)
Saurabh K. Gupta
Saurabh K. Gupta
author image
Saurabh K. Gupta

Saurabh K. Gupta is a seasoned database technologist with extensive experience in designing high performance and highly available database applications. His technology focus has been centered around Oracle Database architecture, Oracle Cloud platform, Database In-Memory, Database Consolidation, Multitenant, Exadata, Big Data, and Hadoop. He has authored the first edition of this book. He is an active speaker at technical conferences from Oracle Technology Network, IOUG Collaborate'15, AIOUG Sangam, and Tech Days. Connect with him on his twitter handle (or SAURABHKG) or through his technical blog www.sbhoracle. wordpress.com, with comments, suggestions, and feedback regarding this book.
Read more about Saurabh K. Gupta

Right arrow

Chapter 3. Designing PL/SQL Code

The structure of a PL/SQL block is one of the elementary components of PL/SQL as it showcases its modeling capabilities. It enables users to declare variables, include procedural constructs in the executable section, and embed exception management within the program.

All SQL statements within a PL/SQL block are executed as a cursor. Cursors are PL/SQL constructs that enable interaction with the data within a PL/SQL block. Cursor designing is an important skill in PL/SQL programming as it impacts the data access paradigm and also code performance. In this chapter, we are going to focus our discussion on cursors. Here is the chapter outline:

  • Cursor fundamentals

    1. How cursors work?

    2. Implicit and explicit cursors

    3. Cursor attributes

    4. Cursor design guidelines

  • Cursor variables

  • Implicit REF CURSOR parameter binding

  • Introduction to subtypes

Cursor structures


In PL/SQL, a cursor structure allows the processing of a SELECT statement and accesses the result returned by that query. Each and every SQL statement in a PL/SQL block is a cursor. A cursor is a handle to the chunk of the memory area where the SQL statements are processed and the result is stored. For a dedicated database, the chunk of memory is in the User Global Area (UGA) while, for shared server connections, the cursor context area is allocated in the System Global Area (SGA).

Cursors can be of two types:

  • Implicit cursors: Every SQL query in the executable or exception section of a PL/SQL block is an implicit cursor. SELECT..INTO, SELECT..BULK COLLECT INTO, SELECT in CURSOR FOR loop, INSERT, UPDATE, DELETE, and MERGE are implicit cursors.

  • Explicit cursors: A cursor defined by the user or developer in the declaration section of a PL/SQL program is an explicit cursor.

Cursor execution cycle

A cursor is a handler to execute an SQL query and lives for the life of a session...

Cursor variables


A cursor variable enables a cursor handler to be associated with multiple SQL queries. With respect to functionality, it is similar to an explicit cursor but with certain implementation changes. One of the fundamental differences is that, unlike a cursor, it is a variable of a cursor type. Therefore, it can potentially be referenced in a similar way to other program variables.

As a variable, it can be passed as a parameter to subprograms or used as a return type of a PL/SQL function. Cursor variables can be quite handy when sharing result sets between two subprograms or when a client pulls a data set from the database.

Cursor variables are created by defining a variable of the REF CURSOR type variable or an SYS_REFCURSOR type variable.

Note

Cursor FOR loop does not support cursor variables

The REF CURSOR syntax is as follows:

TYPE [CURSOR VARIABLE NAME] IS REF CURSOR [RETURN (return type)]

In the preceding syntax, the RETURN type of a cursor variable must be a record type. It...

Implicit statement results in Oracle Database 12c


Oracle Database 12c allows a stored subprogram to return a result set implicitly using the DBMS_SQL package, and not just through the REF CURSOR variable. The new functionality is designed to ease the migration of non-Oracle application programs to Oracle.

Prior to this enhancement in Oracle Database 12c, the only way a PL/SQL stored subprogram could share a result set was through OUT REF CURSOR parameters. Later, parameter binding was required at the client end to retrieve the result sets.

The cursor is returned to the calling environment using new overloaded subprograms: RETURN_RESULT and GET_NEXT_RESULT. The GET_NEXT_RESULT can be used if the cursor query returns multiple result sets. The prototype for RETURN_RESULT is as follows:

PROCEDURE RETURN_RESULT (param_res IN OUT SYS_REFCURSOR,
                         to_client IN BOOLEAN DEFAULT TRUE);

PROCEDURE RETURN_RESULT (param_res IN OUT INTEGER,
                         to_client IN BOOLEAN...

Subtypes


A subtype is a data type that gets evolved from an existing scalar data type. The purpose of creating subtypes, though not mandatory, is to customize the primary data types by controlling certain features such as nullability, range, or sign. An unconstrained subtype is often used in place of primary data types to maintain application standards.

The subtype inherits the behavior of its parent base type and extends it further by a distinguishing feature. For example, NATURALN is a subtype of BINARY_INTEGER, which prevents the entry of nulls and negative values. Similarly, SIGNTYPE permits only three fixed values: -1, 0, or 1.

The following table shows the base types and subtypes under each scalar data type:

Summary


In this chapter, we discussed the importance of cursor structures in PL/SQL code. We covered the working of a cursor, the execution cycle, design considerations, and guidelines. The usage of cursors can be imperative while developing PL/SQL applications.

In the next chapter, we shall cover composite data types, and you will understand how collections can boost PL/SQL code performance and perform data caching.

Practice exercise


  • What are the possible reasons that cause the INVALID_CURSOR exception to occur?

    1. Cursor result set has not been fetched.

    2. The cursor does not have parameters.

    3. The value of the %ROWCOUNT attribute has been referenced after closing the cursor.

    4. Cursor result set has been fetched into a non matching variable.

  • Identify the guidelines to be considered when designing cursors in a PL/SQL block:

    1. Explicit cursors must be used irrespective of the number of records returned by the query.

    2. Cursor FOR loops must be used as it implicitly takes care of OPEN, FETCH, and CLOSE stages.

    3. Cursor data must be fetched as a record.

    4. Use ROWNUM to index the records in the cursor result sets.

  • While processing DMLs as implicit cursors in a PL/SQL executable block, implicit cursor attributes can be used anywhere in the block.

    1. True.

    2. False.

  • From the following options, identify the two correct statements about the REF CURSOR type?

    1. Ref cursors are reference pointers to cursor objects.

    2. REF CURSOR types can be declared in...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Advanced Oracle PL/SQL Developer's Guide (Second Edition) - Second Edition
Published in: Feb 2016Publisher: ISBN-13: 9781785284809
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Saurabh K. Gupta

Saurabh K. Gupta is a seasoned database technologist with extensive experience in designing high performance and highly available database applications. His technology focus has been centered around Oracle Database architecture, Oracle Cloud platform, Database In-Memory, Database Consolidation, Multitenant, Exadata, Big Data, and Hadoop. He has authored the first edition of this book. He is an active speaker at technical conferences from Oracle Technology Network, IOUG Collaborate'15, AIOUG Sangam, and Tech Days. Connect with him on his twitter handle (or SAURABHKG) or through his technical blog www.sbhoracle. wordpress.com, with comments, suggestions, and feedback regarding this book.
Read more about Saurabh K. Gupta

Number

Character

Date/Time

Boolean

NUMBER

VARCHAR

DATE

BOOLEAN

DECIMAL/DEC

VARCHAR2

INTERVAL

 

DOUBLE PRECISION

NVARCHAR2

TIMESTAMP

FLOAT

CHAR

 

INTEGER/INT

NCHAR

NUMERIC

CHARACTER

REAL

LONG

SMALLINT

LONG RAW

PLS_INTEGER

RAW

BINARY_DOUBLE

ROWID

BINARY_FLOAT...