Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
37
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

SQLCODE Function

SQLCODE Function

The function SQLCODE returns the number code of the most recent exception.

For internal exceptions, SQLCODE returns the number of the associated Oracle error. The number that SQLCODE returns is negative unless the Oracle error is no data found, in which case SQLCODE returns +100.

For user-defined exceptions, SQLCODE returns +1, or a value you assign if the exception is associated with an Oracle error number through pragma

EXCEPTION_INIT.

Syntax

sqlcode_function

SQLCODE

Usage Notes

SQLCODE is only useful in an exception handler. Outside a handler, SQLCODE always returns 0. SQLCODE is especially useful in the OTHERS exception handler, because it lets you identify which internal exception was raised.

You cannot use SQLCODE directly in a SQL statement. Assign the value of SQLCODE to a local variable first.

When using pragma RESTRICT_REFERENCES to assert the purity of a stored function, you cannot specify the constraints WNPS and RNPS if the function calls SQLCODE.

Example

The following example inserts the value of SQLCODE into an audit table:

CREATE TABLE errors (code NUMBER, message VARCHAR2(128), happened TIMESTAMP); DECLARE

name employees.last_name%TYPE; my_code NUMBER;

my_errm VARCHAR2(32000); BEGIN

SELECT last_name INTO name FROM employees WHERE employee_id = -1; EXCEPTION

WHEN OTHERS THEN my_code := SQLCODE; my_errm := SQLERRM;

dbms_output.put_line('Error code ' || my_code || ': ' || my_errm);

--Normally we would call another procedure, declared with PRAGMA

--AUTONOMOUS_TRANSACTION, to insert information about errors. INSERT INTO errors VALUES (my_code, my_errm, SYSTIMESTAMP);

END;

/

DROP TABLE errors;

Related Topics

Exceptions, SQLERRM Function, "Retrieving the Error Code and Error Message: SQLCODE and SQLERRM" on page 10-14.

PL/SQL Language Elements 13-135

SQLERRM Function

SQLERRM Function

The function SQLERRM returns the error message associated with its error-number argument. If the argument is omitted, it returns the error message associated with the current value of SQLCODE. SQLERRM with no argument is useful only in an exception handler. Outside a handler, SQLERRM with no argument always returns the message normal, successful completion.

For internal exceptions, SQLERRM returns the message associated with the Oracle error that occurred. The message begins with the Oracle error code.

For user-defined exceptions, SQLERRM returns the message user-defined exception, unless you used the pragma EXCEPTION_INIT to associate the exception with an Oracle error number, in which case SQLERRM returns the corresponding error message. For more information, see "Retrieving the Error Code and Error Message: SQLCODE and SQLERRM" on page 10-14.

Syntax

sqlerrm_function

( error_number )

SQLERRM

Keyword and Parameter Description

error_number

A valid Oracle error number. For a list of Oracle errors (ones prefixed by ORA-), see

Oracle Database Error Messages.

Usage Notes

SQLERRM is especially useful in the OTHERS exception handler, where it lets you identify which internal exception was raised.

The error number passed to SQLERRM should be negative. Passing a zero to SQLERRM always returns the following message:

ORA-0000: normal, successful completion

Passing a positive number to SQLERRM always returns the message

User-Defined Exception

unless you pass +100, in which case SQLERRM returns the following message:

ORA-01403: no data found

You cannot use SQLERRM directly in a SQL statement. Assign the value of SQLERRM to a local variable first:

my_sqlerrm := SQLERRM;

...

INSERT INTO errors VALUES (my_sqlerrm, ...);

When using pragma RESTRICT_REFERENCES to assert the purity of a stored function, you cannot specify the constraints WNPS and RNPS if the function calls SQLERRM.

13-136 PL/SQL User's Guide and Reference

SQLERRM Function

Example

The following example retrieves the error message associated with an unhandled exception, and stores it in an audit table. The SUBSTR function truncates the message if it is too long to fit in the table.

CREATE TABLE errors (code NUMBER, message VARCHAR2(128), happened TIMESTAMP); DECLARE

name employees.last_name%TYPE; my_code NUMBER;

my_errm VARCHAR2(32000); BEGIN

SELECT last_name INTO name FROM employees WHERE employee_id = -1; EXCEPTION

WHEN OTHERS THEN my_code := SQLCODE; my_errm := SQLERRM;

dbms_output.put_line('Error code ' || my_code || ': ' || my_errm);

--Normally we would call another procedure, declared with PRAGMA

--AUTONOMOUS_TRANSACTION, to insert information about errors. INSERT INTO errors VALUES (my_code, my_errm, SYSTIMESTAMP);

END;

/

DROP TABLE errors;

Related Topics

Exceptions, SQLCODE Function

PL/SQL Language Elements 13-137

TIMESTAMP_TO_SCN Function

TIMESTAMP_TO_SCN Function

Syntax

return_value := TIMESTAMP_TO_SCN(timestamp);

Purpose

TIMESTAMP_TO_SCN takes an argument that represents a precise time, and returns the system change number (SCN) of the database at that moment in time. The returned value has the datatype NUMBER.

Usage Notes

This function is part of the flashback query feature. System change numbers provide a precise way to specify the database state at a moment in time, so that you can see the data as it was at that moment.

Call this function to find out the system change number associated with the date and time to which you want to "flash back".

Examples

DECLARE

right_now TIMESTAMP; yesterday TIMESTAMP; sometime TIMESTAMP; scn1 INTEGER; scn2 INTEGER; scn3 INTEGER;

BEGIN

--Get the current SCN. right_now := SYSTIMESTAMP;

scn1 := TIMESTAMP_TO_SCN(right_now); dbms_output.put_line('Current SCN is ' || scn1);

--Get the SCN from exactly 1 day ago. yesterday := right_now - 1;

scn2 := TIMESTAMP_TO_SCN(yesterday); dbms_output.put_line('SCN from yesterday is ' || scn2);

--Find an arbitrary SCN somewhere between yesterday and today.

--(In a real program we would have stored the SCN at some significant moment.) scn3 := (scn1 + scn2) / 2;

--Find out what time that SCN was in effect.

sometime := SCN_TO_TIMESTAMP(scn3);

dbms_output.put_line('SCN ' || scn3 || ' was in effect at ' || TO_CHAR(sometime));

END;

/

Related Topics

SCN_TO_TIMESTAMP Function

13-138 PL/SQL User's Guide and Reference

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в папке Oracle 10g