Avoiding collection exceptions, PL-SQL Programming

Assignment Help:

Avoiding Collection Exceptions 

In many cases, if you reference a nonexistent collection element, then PL/SQL raises a predefined exception. Consider the illustration shown below:

DECLARE

TYPE NumList IS TABLE OF NUMBER;

nums NumList; -- atomically null

BEGIN

/* Assume execution continues despite the raised exceptions. */

nums(1) := 1; -- raises COLLECTION_IS_NULL (1)

nums := NumList(1,2); -- initialize table

nums(NULL) := 3 -- raises VALUE_ERROR (2)

nums(0) := 3; -- raises SUBSCRIPT_OUTSIDE_LIMIT (3)

nums(3) := 3; -- raises SUBSCRIPT_BEYOND_COUNT (4)

nums.DELETE(1); -- delete element 1

IF nums(1) = 1 THEN ... -- raises NO_DATA_FOUND (5)

In the first situation, the nested table is automatically null. In the second situation, the subscript is null. In the third situation, the subscript is outside the legal range. In the fourth situation, the subscripts exceed the number of elements in the table. In the fifth situation, the subscript designates a deleted element.

The list below shows when a given exception is raised:

2127_collection exception.png

In many cases, you can pass "invalid" subscripts to a method without raising the exception. For illustration, if you pass a null subscript to the procedure DELETE, it does nothing. You can also replace the deleted elements without raising NO_DATA_FOUND, as the example below shows:

DECLARE

TYPE NumList IS TABLE OF NUMBER;

nums NumList := NumList(10,20,30); -- initialize table

BEGIN

...

nums.DELETE(-1); -- does not raise SUBSCRIPT_OUTSIDE_LIMIT

nums.DELETE(3); -- delete 3rd element

DBMS_OUTPUT.PUT_LINE(nums.COUNT); -- prints 2

nums(3) := 30; -- legal; does not raise NO_DATA_FOUND

DBMS_OUTPUT.PUT_LINE(nums.COUNT); -- prints 3

END;

The Packaged collection types and the local collection types are never compatible. For example, assume that you want to call the following packaged process:

CREATE PACKAGE pkg1 AS

TYPE NumList IS VARRAY(25) OF NUMBER(4);

PROCEDURE delete_emps (emp_list NumList);

...

END pkg1;

CREATE PACKAGE BODY pkg1 AS

PROCEDURE delete_emps (emp_list NumList) IS ...

...

END pkg1;

If you run the PL/SQL block below, then the second procedure call fails with a wrong number or types of arguments error. This is because the packaged and local VARRAY types are incompatible even though their definitions are same.

DECLARE

TYPE NumList IS VARRAY(25) OF NUMBER(4);

emps pkg1.NumList := pkg1.NumList(7369, 7499);

emps2 NumList := NumList(7521, 7566);

BEGIN

pkg1.delete_emps(emps);

pkg1.delete_emps(emps2); -- causes a compilation error

END;


Related Discussions:- Avoiding collection exceptions

Closing a cursor variable, Closing a Cursor Variable The CLOSE stateme...

Closing a Cursor Variable The CLOSE statement disables the cursor variable. After that, the related result set is undefined. The syntax for the same is as shown below: CLOS

Sql script to create and populate the tables, Create the four tables and po...

Create the four tables and populate them with the given data. Answer the following queries in SQL. 1. Get all part-color/part-city combinations. Note: Here and subsequently, the

Develop a job management site, Lightweight system to provide and take info ...

Lightweight system to provide and take info from workers in the field and office, have basic design outlined already just require build and implementation Desired Skills CSS,

Iterative control:exit statements, EXIT The EXIT statement forces a loop...

EXIT The EXIT statement forces a loop to done unconditionally. Whenever an EXIT statement is encountered, the loop is done immediately and controls the passes to the next statem

Remote operations in pl sql, Remote Operations: As the illustration sh...

Remote Operations: As the illustration shows below, the PL/SQL subprograms can execute the dynamic SQL statements which refer to the objects on a remote database: PROCEDURE

Defining autonomous transactions, Defining Autonomous Transactions To ...

Defining Autonomous Transactions To define an autonomous transaction, you use the pragma (compiler directive) AUTONOMOUS_TRANSACTION. The pragma instructs the PL/SQL compiler

Semidifference via except and join - sql, Semidifference via EXCEPT and JOI...

Semidifference via EXCEPT and JOIN - SQL SELECT * FROM (SELECT StudentId FROM IS_CALLED WHERE Name = 'Devinder' EXCEPT DISTINCT CORRESPONDING SELECT StudentId

Obtaining a natural join by specifying the common columns, Obtaining a natu...

Obtaining a natural join by specifying the common columns Synatax: SELECT * FROM IS_CALLED JOIN IS_ENROLLED_ON USING ( StudentId ) However, a named columns join doe

In mode - parameter modes, IN Mode An IN parameter pass the values to ...

IN Mode An IN parameter pass the values to the subprogram being called. Within the subprogram, an IN parameter acts like a constant. And hence, it cannot be assigned a value.

Implicit cursor attributes, Implicit Cursor Attributes The Implicit cur...

Implicit Cursor Attributes The Implicit cursor attributes returns the information about the execution of an INSERT, DELETE, UPDATE, or SELECT INTO statement. The cursor attribu

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd