PLSQL 7 2 SG
PLSQL 7 2 SG
Updates:
1
2
3
4
5
An Oracle Server error is an error which is recognized and raised automatically by the Oracle server. There are many thousands of such possible errors, and each one
has a predefined error number and error message.
User-defined errors (raised explicitly by the PL/SQL programmer) are explained in the next lesson, but if students are curious at this point, here is an example:
We want to INSERT a new row into DEPARTMENTS, but want to check in advance if the new department_id (a unique column) is already used (this is often done in real
applications). We could code:
DECLARE
v_dept_id departments.department_id%TYPE := 10; -- This department exists already
…
BEGIN
SELECT department_id INTO v_dept_id
FROM departments WHERE department_id = v_dept_id;
…
INSERT INTO departments VALUES (v_dept_id, ….);
…
Because department 10 already exists, the SELECT successfully returns exactly one row and therefore no Oracle Server exception is raised, and the Oracle Server
attempts to continue executing the rest of the block. But clearly we do not want to execute the INSERT in this case; instead, we want to display an error message to the
user. We would declare and raise our own user_defined (or programmer_defined) exception.
6
7
8
9
10
11
An Oracle Server error is an error which is recognized and raised automatically by the Oracle server. There
are many hundreds of such possible errors, and each one has a predefined error number and error
message, for example ORA-01422: exact fetch returns more than requested number of rows.
The most common 20 or so server errors have predefined PL/SQL exception names, for example
TOO_MANY_ROWS (= ORA-01422), NO_DATA_FOUND (= ORA-01403).
12
13
14
This example handles TOO_MANY_ROWS and NO_DATA_FOUND errors, with a WHEN OTHERS handler in
case any other error occurs.
Remind students that a WHEN OTHERS handler will handle all types of raised exceptions: predefined,, non-
predefined and user_defined.
15
16
17
18
Point out that the EXCEPTION section can refer to exceptions only by name, not by number (i.e. we cannot
code WHEN ORA-01400 THEN ….). There is no predefined PL/SQL name for the ORA-1400 exception, so we
need to create our own name for it. The next slide shows how to do this.
19
20
21
22
23
24
Students may ask: why is the SQLCODE for NO_DATA_FOUND +100 instead of -01403?
Answer: some of these error codes are used in other (non-Oracle) databases, and +100 is an
internationally-agreed code when no rows are returned from a query.
25
SQLCODE and SQLERRM are often used in a WHEN OTHERS handler. Someone (often the Database
Administrator) would be responsible for reading the ERROR_LOG table and taking suitable action.
Ask students: why can’t we use SQLCODE or SQLERRM directly in a SQL statement? Answer: because that
SQL statement (the INSERT INTO error_log in the slide) could also raise an exception, which would change
the values of SQLCODE and SQLERRM.
It is good programming practice to capture SQLERRM and SQLCODE in a WHEN OTHERS and insert the
returned values into an error table, so you can later go back and see what kind of problems your program
encountered, and if the same errors shows up a number of times, then include a specific hander for it.
26
Predefined Oracle Server Errors: Each of these has a predefined name. For example, if the error ORA-
01403 occurs when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the
predefined exception-name NO_DATA_FOUND.
Non-predefined Oracle Server Errors: Each of these has a standard Oracle error number (ORA-nnnnn) and
error message, but not a predefined name. We declare our own names for these so that we can reference
these names in the exception section.
PRAGMA EXCEPTION_INIT – Tells the compiler to associate an exception name with an Oracle error
number. That allows you to refer to any Oracle Server exception by name and to write a specific handler for
it.
SQLERRM – Returns character data containing the message associated with the error number
SQLCODE – Returns the numeric value for the error code (You can assign it to a NUMBER variable.)
27
28
29
30