Exceptions in PLSQL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

SCOTT>>DECLARE

2 D DATE;
3 BEGIN
4 SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=&EID;
5 DBMS_OUTPUT.PUT_LINE(D);
6 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
7 END;
8 /
Enter value for eid: 7900
old 4: SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=&EID;
new 4: SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=7900;
03-DEC-81
GOOD MORNING

PL/SQL procedure successfully completed.

SCOTT>>/
Enter value for eid: 1234
old 4: SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=&EID;
new 4: SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=1234;
DECLARE
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 4

SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 D DATE;
3 BEGIN
4 SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=&EID;
5 DBMS_OUTPUT.PUT_LINE(D);
6 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
7 EXCEPTION
8 WHEN NO_DATA_FOUND THEN
9 DBMS_OUTPUT.PUT_LINE(SQLCODE);
10 DBMS_OUTPUT.PUT_LINE(SQLERRM);
11* END;
SCOTT>>/
Enter value for eid: 7902
old 4: SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=&EID;
new 4: SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=7902;
03-DEC-81
GOOD MORNING

PL/SQL procedure successfully completed.

SCOTT>>/
Enter value for eid: 2145
old 4: SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=&EID;
new 4: SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=2145;
100
ORA-01403: no data found

PL/SQL procedure successfully completed.

SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 D DATE;
3 BEGIN
4 SELECT HIREDATE INTO D FROM EMP WHERE DEPTNO=20;
5 DBMS_OUTPUT.PUT_LINE(D);
6 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
7 EXCEPTION
8 WHEN NO_DATA_FOUND THEN
9 DBMS_OUTPUT.PUT_LINE(SQLCODE);
10 DBMS_OUTPUT.PUT_LINE(SQLERRM);
11* END;
12 /
DECLARE
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 4

SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 D DATE;
3 BEGIN
4 SELECT HIREDATE INTO D FROM EMP WHERE DEPTNO=20;
5 DBMS_OUTPUT.PUT_LINE(D);
6 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
7 EXCEPTION
8 WHEN NO_DATA_FOUND THEN
9 DBMS_OUTPUT.PUT_LINE(SQLCODE);
10 DBMS_OUTPUT.PUT_LINE(SQLERRM);
11 WHEN TOO_MANY_ROWS THEN
12 DBMS_OUTPUT.PUT_LINE(SQLCODE);
13 DBMS_OUTPUT.PUT_LINE(SQLERRM);
14* END;
SCOTT>>/
-1422
ORA-01422: exact fetch returns more than requested number of rows

PL/SQL procedure successfully completed.

SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 D DATE;
3 BEGIN
4 SELECT HIREDATE INTO D FROM EMP WHERE DEPTNO=20;
5 DBMS_OUTPUT.PUT_LINE(D);
6 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
7 EXCEPTION
8 WHEN OTHERS THEN
9 DBMS_OUTPUT.PUT_LINE(SQLCODE);
10 DBMS_OUTPUT.PUT_LINE(SQLERRM);
11* END;
SCOTT>>/
-1422
ORA-01422: exact fetch returns more than requested number of rows

PL/SQL procedure successfully completed.

SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 D DATE;
3 BEGIN
4 SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=1122;
5 DBMS_OUTPUT.PUT_LINE(D);
6 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
7 EXCEPTION
8 WHEN NO_DATA_FOUND THEN
9 DBMS_OUTPUT.PUT_LINE('INVALID EMPNO');
10 DBMS_OUTPUT.PUT_LINE(SQLCODE);
11 DBMS_OUTPUT.PUT_LINE(SQLERRM);
12 WHEN OTHERS THEN
13 DBMS_OUTPUT.PUT_LINE(SQLCODE);
14 DBMS_OUTPUT.PUT_LINE(SQLERRM);
15* END;
16 /
INVALID EMPNO
100
ORA-01403: no data found

PL/SQL procedure successfully completed.

SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 D DATE;
3 BEGIN
4 SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=7900;
5 DBMS_OUTPUT.PUT_LINE(D);
6 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
7 SELECT HIREDATE INTO D FROM EMP WHERE SAL>1000;
8 DBMS_OUTPUT.PUT_LINE(D);
9 EXCEPTION
10 WHEN NO_DATA_FOUND THEN
11 DBMS_OUTPUT.PUT_LINE('INVALID EMPNO');
12 DBMS_OUTPUT.PUT_LINE(SQLCODE);
13 DBMS_OUTPUT.PUT_LINE(SQLERRM);
14 WHEN OTHERS THEN
15 DBMS_OUTPUT.PUT_LINE(SQLCODE);
16 DBMS_OUTPUT.PUT_LINE(SQLERRM);
17* END;
SCOTT>>/
03-DEC-81
GOOD MORNING
-1422
ORA-01422: exact fetch returns more than requested number of rows

PL/SQL procedure successfully completed.

SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 D DATE;
3 BEGIN
4 SELECT HIREDATE INTO D FROM EMP WHERE SAL>1000;
5 DBMS_OUTPUT.PUT_LINE(D);
6 SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=7900;
7 DBMS_OUTPUT.PUT_LINE(D);
8 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
9 EXCEPTION
10 WHEN NO_DATA_FOUND THEN
11 DBMS_OUTPUT.PUT_LINE('INVALID EMPNO');
12 DBMS_OUTPUT.PUT_LINE(SQLCODE);
13 DBMS_OUTPUT.PUT_LINE(SQLERRM);
14 WHEN OTHERS THEN
15 DBMS_OUTPUT.PUT_LINE(SQLCODE);
16 DBMS_OUTPUT.PUT_LINE(SQLERRM);
17* END;
18 /
-1422
ORA-01422: exact fetch returns more than requested number of rows

PL/SQL procedure successfully completed.

SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 D DATE;
3 SEP26 EXCEPTION;
4 PRAGMA EXCEPTION_INIT(SEP26,100);
5 BEGIN
6 SELECT HIREDATE INTO D FROM EMP WHERE EMPNO=1122;
7 DBMS_OUTPUT.PUT_LINE(D);
8 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
9 EXCEPTION
10 WHEN SEP26 THEN
11 DBMS_OUTPUT.PUT_LINE('INVALID EMPNO');
12 DBMS_OUTPUT.PUT_LINE(SQLCODE);
13 DBMS_OUTPUT.PUT_LINE(SQLERRM);
14 WHEN OTHERS THEN
15 DBMS_OUTPUT.PUT_LINE(SQLCODE);
16 DBMS_OUTPUT.PUT_LINE(SQLERRM);
17* END;
SCOTT>>/
INVALID EMPNO
100
ORA-01403: no data found

PL/SQL procedure successfully completed.

SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 A NUMBER;
3 B NUMBER;
4 JAVA EXCEPTION;
5 BEGIN
6 A:=&NO1;
7 B:=&NO2;
8 IF(A>B) THEN
9 DBMS_OUTPUT.PUT_LINE(A+B);
10 ELSE
11 RAISE JAVA;
12 END IF;
13 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
14 EXCEPTION
15 WHEN JAVA THEN
16 DBMS_OUTPUT.PUT_LINE('A MUST BE GREATER');
17* END;
SCOTT>>/
Enter value for no1: 11
old 6: A:=&NO1;
new 6: A:=11;
Enter value for no2: 8
old 7: B:=&NO2;
new 7: B:=8;
19
GOOD MORNING

PL/SQL procedure successfully completed.

SCOTT>>/
Enter value for no1: 11
old 6: A:=&NO1;
new 6: A:=11;
Enter value for no2: 22
old 7: B:=&NO2;
new 7: B:=22;
A MUST BE GREATER

PL/SQL procedure successfully completed.

SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 A NUMBER;
3 B NUMBER;
4 JAVA EXCEPTION;
5 BEGIN
6 A:=&NO1;
7 B:=&NO2;
8 IF(A>B) THEN
9 DBMS_OUTPUT.PUT_LINE(A+B);
10 ELSE
11 -- RAISE JAVA;
12 DBMS_OUTPUT.PUT_LINE('A MUST BE GREATER');
13 END IF;
14 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
15 /*EXCEPTION
16 WHEN JAVA THEN
17 DBMS_OUTPUT.PUT_LINE('A MUST BE GREATER');*/
18* END;
SCOTT>>/
Enter value for no1: 11
old 6: A:=&NO1;
new 6: A:=11;
Enter value for no2: 22
old 7: B:=&NO2;
new 7: B:=22;
A MUST BE GREATER
GOOD MORNING

PL/SQL procedure successfully completed.

SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 A NUMBER;
3 B NUMBER;
4 JAVA EXCEPTION;
5 BEGIN
6 A:=&NO1;
7 B:=&NO2;
8 IF(A>B) THEN
9 DBMS_OUTPUT.PUT_LINE(A+B);
10 ELSE
11 RAISE JAVA;
12 DBMS_OUTPUT.PUT_LINE('A MUST BE GREATER');
13 END IF;
14 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
15 EXCEPTION
16 WHEN JAVA THEN
17 DBMS_OUTPUT.PUT_LINE('A MUST BE GREATER');
18 RAISE_APPLICATION_ERROR(-20999,'A MUST BE BIGGER');
19* END;
SCOTT>>/
Enter value for no1: 11
old 6: A:=&NO1;
new 6: A:=11;
Enter value for no2: 44
old 7: B:=&NO2;
new 7: B:=44;
A MUST BE GREATER
DECLARE
*
ERROR at line 1:
ORA-20999: A MUST BE BIGGER
ORA-06512: at line 18

SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>
SCOTT>>ED
Wrote file afiedt.buf

1 DECLARE
2 S NUMBER;
3 BEGIN
4 SELECT SAL INTO S FROM EMP WHERE EMPNO=7900;
5 DBMS_OUTPUT.PUT_LINE('SALARY IS '||S);
6 DBMS_OUTPUT.PUT_LINE('GOOD MORNING');
7 DECLARE
8 C NUMBER;
9 BEGIN
10 SELECT COMM INTO C FROM EMP WHERE EMPNO=7902;
11 DBMS_OUTPUT.PUT_LINE('COMM IS '||C);
12 DBMS_OUTPUT.PUT_LINE('INNER BLOCK');
13 EXCEPTION
14 WHEN NO_DATA_FOUND THEN
15 DBMS_OUTPUT.PUT_LINE(SQLCODE);
16 DBMS_OUTPUT.PUT_LINE(SQLERRM);
17 END;
18 DBMS_OUTPUT.PUT_LINE('INNER BLOCK OVER');
19 EXCEPTION
20 WHEN NO_DATA_FOUND THEN
21 DBMS_OUTPUT.PUT_LINE(SQLCODE);
22 DBMS_OUTPUT.PUT_LINE(SQLERRM);
23* END;
SCOTT>>SPOOL OFF

You might also like