Tuesday, October 21, 2014

Exception in pl/sql



Exception: are some undesired situations when PL/SQL program terminated unexpectedly.
EXCEPTION HANDLING is used to handle run-time errors. All the Exception Handling code is written in the EXCEPTION section of a PL/SQL program.
DECLARE
/*this block is used for variable declaration, cursor/record definition */
BEGIN
/*this block is used to define the SQL-PL/SQL statements*/
EXCEPTION
WHEN EXCEPTION_NAME THEN
--error handling code
WHEN EXCEPTION_NAME THEN
--error handling code
WHEN OTHERS THEN
--error handling code
END;
There are three types of exceptions. These are:
1. Predefined Exceptions: these are the common errors that have given predefined names, displayed on screen.
Exception Name
Description
NO_DATA_FOUND
When select statement doesn’t return any record
TOO_MANY_ROWS
When select statement return more than one record.
ZERO_DIVIDE
When any no is to be divide by zero.
DUP_VAL_ON_INDEX
Attempt to insert duplicate value.
INVALID_CURSOR
Illegal cursor operation occurred

Write PL/SQL script to input sal amt and display the Emp Name earning same sal amt. Use NO_DATA_FOUND and TOO_MANY ROWS Exception.

DECLARE
SALARY NUMBER;
E_NAME EMP.ENAME%TYPE;
BEGIN
SALARY := &SAL;
SELECT ENAME INTO E_NAME FROM EMP WHERE SAL=SALARY;
DBMS_OUTPUT.PUT_LINE(E_NAME) ;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE(‘INVALID VALUE’);
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE(‘return multiple value’);
END;
2. Undefined Exceptions: these are the less common errors that have not given predefined names. For these errors ORA – error code i.e., the system compilation error messages are displayed.
3. User-defined Exceptions: errors that do not cause a run-time error, but will violate business rules. Such errors are handled through programmer created custom error messages. User-defined Exception is raised with RAISE .

Write PL/SQL script that traps ZERO_DIVIDE exception when a no is divided by other no. also raised user define exception if no2 greater than no1.

DECLARE
NO1 NUMBER;
NO2 NUMBER;
N2_GT_N1 EXCEPTION;    --user-defined exception
BEGIN
NO1 := &N1;
NO2 := &N2;
IF NO2 > NO1 THEN
RAISE N2_GT_N1;   --user-defined exception raised.
END IF;
DBMS_OUTPUT.PUT_LINE(NO1 / NO2);
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE(‘not divide by zero’);
WHEN N2_GT_N1 THEN
DBMS_OUTPUT.PUT_LINE(‘no2 greater than no1’);
END;

PL/SQL supports three types of records



Records: PL/SQL Record is a combination of fields. Records are used to represent complete row in a table. ‘Record is a group of multiple piece of information, related to one another, called fields’.
PL/SQL supports three types of records:
Table Based Record represents each field in the table. Fields in the record have the same name and datatype as the columns in the table have. Table Based Record is declared with an anchored declaration-using %ROWTYPE.

Write the PL/SQL code to display the ename and sal of emp whose empno is input by user, using table-based record.
DECLARE
RS EMP%ROWTYPE;
BEGIN
SELECT * INTO RS FROM EMP WHERE EMPNO=&ENO;
DBMS_OUTPUT.PUT_LINE(RS.ENAME ||RS.SAL);
END;

Programmer Defined Record is declared and defined explicitly by the programmer as per requirement. Firstly a RECORD type is defined and then variable of that RECORD type is declared.
Write the PL/SQL code to display the ename and sal of emp whose empno is input by user, using programmer-based record.
DECLARE
TYPE RS_EMP IS RECORD
(
EMP_NAME EMP.ENAME%TYPE,
EMP_SAL EMP.SAL%TYPE
);
RS RS_EMP;
BEGIN
SELECT ENAME, SAL INTO RS FROM EMP WHERE EMPNO=&ENO;
DBMS_OUTPUT.PUT_LINE(RS.EMP_NAME);
DBMS_OUTPUT.PUT_LINE(RS.EMP_SAL);
END;

Cursor Based Record is composite variable that represents each field in a cursor.  The fields of record have same name and datatype as the fields of the cursor on which the record is based. It is declared using %ROWTYPE
 

Write PL/SQL code to insert the record in dept table.




DECLARE
DNO NUMBER;
DEPT_NAME DEPT.DNAME%TYPE;
DLOC DEPT.LOC%TYPE;
BEGIN
DNO := &DEPTNO;
DEPT_NAME := ‘&DNAME’;
DLOC := ‘&LOCATION’;
INSERT INTO DEPT VALUES(DNO, DEPT_NAME, DLOC);
DBMS_OUTPUT.PUT_LINE(‘RECORD INSERT’);
END;

Write the PL/SQL code to input the emp’s no and increase the salary by 10% if his salary < 5000 otherwise delete the record.




DECLARE
ENO NUMBER;
SALARY NUMBER;
BEGIN
ENO := &EMPNO;
SELECT SALARY INTO SAL FROM EMP WHERE EMPNO = ENO;
IF SALARY < 5000 THEN
UPDATE EMP SET SAL = SAL + SAL *.1 WHERE EMPNO = ENO;
DBMS_OUTPUT.PUT_LINE(‘SALARY UPDATE’);
ELSE
DELETE FROM EMP WHERE EMPNO = ENO;
DBMS_OUTPUT.PUT_LINE(‘RECORD DELETED’);
END IF;
END;