Exp 8
Exp 8
Program Development using WHILE LOOPS, Numeric FOR LOOPS, Nested Loops using ERROR
Handling, BUILT-IN Exceptions, USE Defined Exceptions, RAISE- APPLICATION ERROR
LOOPING STATEMENTS:- For executing the set of statements repeatedly we can use loops.
The oracle supports number of looping statements like GOTO, FOR, WHILE & LOOP. Here is the
syntax of these all the types of looping statements.
• GOTO STATEMENTS
<<LABEL>>
SET OF STATEMENTS
GOTO LABEL;
• FOR LOOP
FOR <VAR> IN [REVERSE] <INI_VALUE>..<END_VALUE>
SET OF STATEMENTS
END LOOP;
• WHILE LOOP
WHILE (CONDITION) LOOP
SET OF STATEMENTS
END LOOP;
• LOOP STATEMENT
LOOP
SET OF STATEMENTS
IF (CONDITION) THEN
EXIT
SET OF STATEMENTS
END LOOP;
While using LOOP statement, we have to take care of EXIT condition; otherwise it may go into
infinite loop.
Example :- Here are the example for all these types of looping statement where each program
prints numbers 1 to 10.
GOTO EXAMPLE
DECLARE
I INTEGER := 1;
BEGIN
<<OUTPUT>>
DBMS_OUTPUT.PUT_LINE(I);
I := I + 1;
IF I<=10 THEN
GOTO OUTPUT;
END IF;
END;
/
Output :
FOR LOOP EXAMPLE
BEGIN
FOR I IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(I);
END LOOP;
END;
/
Output :
WHILE EXAMPLE
DECLARE
I INTEGER := 1;
BEGIN
WHILE(I<=10) LOOP
DBMS_OUTPUT.PUT_LINE(I);
I := I + 1;
END LOOP;
END;
/
Output :
LOOP EXAMPLE
DECLARE
I INTEGER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE(I);
I := I + 1;
EXIT WHEN I=11;
END LOOP;
END;
/
Output :
Write PL/SQL Program to display (Using different looping statements)
**
***
****
Program
Output :
DATA TYPES
We know basic data types in Oracle. Now let‘s see few more data types that are useful for writing
PL/SQL programs in Oracle.
%TYPE :- %TYPE is used to give data type of predefined variable or database column.
Eg:- itemcode Number(10);
icode itemcode%Type;
The database column can be used as
id Sailors.sid%type
%ROWTYPE :- %rowtype is used to provide record datatype to a variable. The variable
can store row of the table or row fetched from the cursor.
Eg:- If we want to store a row of table Sailors then we can declare variable
as Sailors%Rowtype
Comments :- In Oracle we can have two types of comments i.e Single Line & Multiline comments.
• Single line comment :- It starts with --.
-- Comment here
• Multiline comment is same as C/C++/JAVA comments where comments are present in the
pair of /* & */.
/* Comment here */
Inserting values to table :- Here is the example for inserting the values into a database through
PL/SQL Program. Remember that we have to follow all the rules of SQL like Primary Key
Constraints, Foreign Key Constraints, Check Constraints, etc. Ex:- Insert the record into Sailors
table by reading the values from the Keyboard.
DECLARE
SID NUMBER (5):=&SID;
SNAME VARCHAR2(30):='&SNAME';
RATING NUMBER(5):=&RATING;
AGE NUMBER(4,2):=&AGE;
BEGIN
INSERT INTO SAILORS VALUES(SID, SNAME, RATING, AGE);
END;
/
Output :
Reading from table
DECLARE
SID VARCHAR2(10); -- or can be defined SID Sailors.SID%Type
SNAME VARCHAR2(30);
RATING NUMBER(5);
AGE NUMBER(4,2);
BEGIN
SELECT SID, SNAME, RATING, AGE INTO SID, SNAME, RATING, AGE FROM SAILORS
WHERE SID='&SID';
DBMS_OUTPUT.PUT_LINE(SID || ' '|| SNAME || ' '|| RATING ||' '|| AGE );
END;
/
Output :
Some Points regarding SELECT --- INTO
We have to ensure that the SELECT….INTO statement should return one & only one row. If no
row is selected then exception NO_DATA_FOUND is raised. If more than one row is selected then
exception TOO_MANY_ROWS is raised.
To handle the situation where no rows selected or so many rows selected we can use
Exceptions. We have two types of exception, User-Defined and Pre-Defined Exceptions.
DECLARE
N INTEGER:=&N;
A EXCEPTION;
B EXCEPTION;
BEGIN
IF MOD(N,2)=0
THEN RAISE A;
ELSE RAISE B;
END IF;
EXCEPTION
WHEN A THEN
DECLARE
SID VARCHAR2(10);
BEGIN
SELECT SID INTO SID FROM SAILORS WHERE SNAME='&SNAME';
DBMS_OUTPUT.PUT_LINE (SID);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE (‗No Sailors with given SID found‘);
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE (‗More than one Sailors with same name
found‘); END;
/
Output :
Error Handling using RAISE_APPLICATION_ERROR
Functions in PL/SQL
Similar to Procedure we can create Functions which can return one value to the calling
program. The syntax of function is
CREATE OR REPLACE FUNCTION <Fun_Name> (Par_Name1 [IN/OUT/ IN
OUT] Par_Type1, ….) RETURN
return_datatype IS Local
declarations;
BEGIN
PL/SQL Executable statements;
…….
EXCEPTION
Exception Handlers;
END <Fun_Name>;