Using first and last - collection method, PL-SQL Programming

Using FIRST and LAST

FIRST and LAST return the first and last (minimum and maximum) index numbers in a collection. When the collection is empty, the FIRST and LAST return NULL. If the collections contains only one element, the FIRST and LAST return the same index number, as the example below shows:

IF courses.FIRST = courses.LAST THEN ... -- only one element

The later example shows that you can use FIRST and LAST to specify the lower and upper bounds of a loop range provided each element in that range exists:

FOR i IN courses.FIRST..courses.LAST LOOP ...

However, you can use the FIRST or LAST where the integer expression is allowed. In the example below, you use FIRST to initialize the loop counter:

i := courses.FIRST;

WHILE i IS NOT NULL LOOP...

For varrays, the FIRST always returns 1 and LAST always equals to COUNT. For the nested tables, FIRST generally returns 1. But, if you delete elements from the beginning of the nested table, FIRST returns a number bigger than 1. Also for the nested tables, the LAST generally equals to COUNT. But, if you delete elements from the middle of a nested table, the LAST becomes larger than COUNT.

Posted Date: 10/4/2012 3:15:27 AM | Location : United States







Related Discussions:- Using first and last - collection method, Assignment Help, Ask Question on Using first and last - collection method, Get Answer, Expert's Help, Using first and last - collection method Discussions

Write discussion on Using first and last - collection method
Your posts are moderated
Related Questions
Functions   The function is a subprogram that calculates a value. The Functions and procedures are structured similar, except that the functions have a RETURN clause. You can

PL SQL Code Review HEADER ELEMENTS File Name Clear, meaningful and descriptive about main objective of the file. Multiple words are joined using underscores which adh

Special cases of projection This section describes the identity projection, r {ALL BUT}, and the projection on no attributes, r { }, which yields TABLE_DUM when r is empty, ot

Using Cursor Attributes: Every cursor has 4 attributes: %NOTFOUND, %FOUND, %ISOPEN, and %ROWCOUNT. If appended to the cursor name, they return the helpful information about

Accessing Attributes: You can refer to an attribute only by its name not by its position in the object type. To access or modify the value of an attribute, you can use the dot

Advantages of Wrapping   The PL/SQL Wrapper convert the PL/SQL source code into a transitional form of the object code. By hiding the application internals, the Wrapper secure

Explicit Cursor Attributes The cursor variable or each cursor has four attributes: %FOUND, %ISOPEN, %ROWCOUNT, and %NOTFOUND. When appended to the cursor or cursor variable, th

set serveroutput on declare a number(5); b number(5); c number(5); begin a:=&a; b:=&b; c:=a/b; dbms_output.put_line(c); exception when zero_d

Using DEFAULT You can use the keyword DEFAULT rather than that of the assignment operator to initialize the variables. For e.g. the declaration blood_type CHAR := ’O’; it can b

Packaging Cursors   You can split a cursor specification from its body for placement in a package. In that way, you can change the cursor body without changing the cursor spec