0% found this document useful (0 votes)
18 views

Session 3 PDF

The document discusses exceptions in programming and provides examples of how to handle errors using exceptions in Oracle PL/SQL. It explains common exceptions like NO_DATA_FOUND and TOO_MANY_ROWS. It demonstrates how to use exceptions like when too_many_rows and when no_data_found to handle errors instead of getting default error messages. The examples show how to raise custom errors using raise_application_error when an exception occurs.

Uploaded by

MazMoh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Session 3 PDF

The document discusses exceptions in programming and provides examples of how to handle errors using exceptions in Oracle PL/SQL. It explains common exceptions like NO_DATA_FOUND and TOO_MANY_ROWS. It demonstrates how to use exceptions like when too_many_rows and when no_data_found to handle errors instead of getting default error messages. The examples show how to raise custom errors using raise_application_error when an exception occurs.

Uploaded by

MazMoh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Session 3

Exceptions

:
NO_DATA_FOUND

TOO_MANY_ROWS

INVALID_CURSOR

ZERO_DIVIDE


exact fetch returns more than requested
number of rows


:

Declare

; number

Begin
; Select empno into x from emp
; End

Ahmed Alsaied
[email protected]

[email protected]


Session 3

exact fetch returns more than


requested number of rows
:
Declare
; number

Begin

; Select empno into x from emp


then

Exception when too_many_rows

; )'Dbms_output.put_line('you must assign one value

; End

when too many


rows
.

when no data found



:

Declare

; number

Begin

Select empno into x from emp


; Where empno = 18
; End

Ahmed Alsaied
[email protected]

[email protected]


Session 3

no data found

when no data found
.
Declare
; number

Begin
Select empno into x from emp

; Where empno = 18
Exception when no_data_found then

Raise_application _error(-20101 ' There is no data to put


; )' in the variable

;End


raise application error



-20101
20000 20999 .

user defined exception



If

:
Declare
Ahmed Alsaied
[email protected]

[email protected]


Session 3

; exception

BEGIN
IF sysdate > '20-april-2009' then
; Raise x
; End if

Exception
When x then
; )'Dbms_output.put_line('user-defined exception

; End








procedures


. SQL

anonymous

declaration begin
& end . exception

Ahmed Alsaied
[email protected]

[email protected]


Session 3

: procedure
create
or replace

procedure
is begin
:

Create or replace procedure test1

; emp.sal%type

Is
V_sal
Begin

Select max(sal) into v_sal from emp

; Where empno = 7788


;End


procedure created
anonymous block
declare anonymous block
is procedure
anonymous block

Ahmed Alsaied
[email protected]

[email protected]


Session 3


.
:
sqlplus execute
. exec
:

-1
Create procedure P1
IS
Cursor xemp is
Select ename , sal from emp

V_sal emp.sal%type;

V_ename emp.ename%type ;
Begin

Open xemp

Fetch xemp into v_ename , v_sal ;


Dbms_output.put_line('Employee Name is :' || v_ename )
;
Dbms_output.put_line('Employee Sal is :' || v_sal ) ;
Close xemp ;
End p1

[email protected]

Ahmed Alsaied
[email protected]


Session 3

:
parameters
procedures
procedure
:
: In Parameters
default

:

Create or replace procedure test2 (p_empno in


)emp.empno%type

Is

; V_sal emp.sal%type
Begin

Select sal into v_sal

From emp

; Where emmpno = p_empno


; )Dbms_output.put_line(v_sal
; End
/
Procedure careated .

empno emp
Ahmed Alsaied
[email protected]

[email protected]


Session 3

where

V_SAL V_SAL
:
)Exec test2(7788


7788 .

CREATE OR REPLACE PROCEDURE P5 (P_EMPNO


)EMP.EMPNO%TYPE
IS
BEGIN
UPDATE EMP SET
SAL = SAL*1.10
; WHERE EMPNO = P_EMPNO
; END P5

; )EXECUTE P5 (7788

: Out Parameters



, :
Create or replace procedure test3
(p_empno in emp.empno%type , p_ename out
)emp.ename%type
Is
Ahmed Alsaied
[email protected]

[email protected]


Session 3

; V_ename emp.ename%type
Begin
Select ename into v_ename
From emp
; Where empno = p_empno

; P_ename = :v_ename
; End


p_empno
v_ename v_ename
.. p_ename

p_ename
:
)Variable x varcahr2 (20
Print x


) ( :
)Exec test3 (7902 , :x

:
CREATE OR REPLACE PROCEDURE P6
(V_ID
IN EMP.EMPNO%TYPE ,
V_NAME OUT EMP.ENAME%TYPE ,
V_SAL
OUT EMP.SAL%TYPE
,
V_COMM OUT EMP.COMM%TYPE
)
IS

Ahmed Alsaied
[email protected]

[email protected]


Session 3

BEGIN
SELECT ENAME , SAL , COMM
INTO
V_NAME, V_SAL,V_COMM
FROM EMP
WHERE EMPNO = V_ID ;
END P6;
SQL> VARIABLE g_name VARCHAR2(15)
SQL> VARIABLE g_sal NUMBER
SQL> VARIABLE g_comm NUMBER
SQL> EXECUTE query_emp(7654,:g_name,
:g_sal, :g_comm)
SQL> PRINT g_name

. drop procedure

: show error

10

[email protected]

Ahmed Alsaied
[email protected]

You might also like