Errors and messages

更新时间:
复制 MD 格式

Use DBMS_OUTPUT.PUT_LINE to write messages to the output buffer, and use the special variables SQLCODE and SQLERRM to inspect error details.

Report messages

DBMS_OUTPUT.PUT_LINE accepts any expression that evaluates to a string and writes it to the session output buffer.

Syntax

DBMS_OUTPUT.PUT_LINE ( message );
ParameterDescription
messageAny expression that evaluates to a string.

Example

BEGIN
    DBMS_OUTPUT.PUT_LINE('My name is John');
END;

Retrieve error details

SQLCODE and SQLERRM contain a numeric code and a text message that describe the outcome of the last SQL statement issued. If any other error occurs in the program, such as division by zero, these variables contain information pertaining to the error.

VariableTypeDescription
SQLCODENUMBERNumeric error code of the most recent SQL statement
SQLERRMText messageError message text of the most recent SQL statement

Example: log error details in an exception handler

DECLARE
    v_code  NUMBER;
    v_errm  VARCHAR2(512);
BEGIN
    -- Trigger a division-by-zero error
    INSERT INTO logs (result) VALUES (1 / 0);
EXCEPTION
    WHEN OTHERS THEN
        v_code := SQLCODE;
        v_errm := SQLERRM;
        DBMS_OUTPUT.PUT_LINE('Error code: ' || v_code);
        DBMS_OUTPUT.PUT_LINE('Error message: ' || v_errm);
END;