Sunday, 14 February 2016

Write a PL/SQL block to take an input in the form of department number. Print department information if the department number is present in the DEPT table else print appropriate message and terminate the block.

Write a PL/SQL block to take an input in the form of department number. 
Print department information if the department number is present in the DEPT
 table else print appropriate message and terminate the block.

DECLARE
NO NUMBER(2);
CNT NUMBER;
DNO DEPT.DEPTNO%TYPE;
DNM DEPT.DNAME%TYPE;
DLOC DEPT.LOC%TYPE;
NO_ROWS EXCEPTION;
BEGIN
NO:=&NO;
SELECT COUNT(*) INTO CNT FROM DEPT WHERE DEPTNO=NO;
IF CNT=0 THEN
 RAISE NO_ROwS;
ELSE
 SELECT DEPTNO, DNAME, LOC INTO DNO, DNM, DLOC FROM DEPT WHERE DEPTNO=NO;
 DBMS_OUTPUT.PUT_LINE(DNO||' '||DNM||' '||DLOC);
END IF;
EXCEPTION
WHEN NO_ROWS THEN
 DBMS_OUTPUT.PUT_LINE('NO DATA FOUND.');
END;
/

No comments:

Post a Comment