Sub topic 3
Printing Results with DBMS_OUTPUT
Assume you already know basic SQL, PL/SQL block structure, and how to run a script in SQL*Plus, SQLcl, or SQL Developer. This chapter focuses on observing what a PL/SQL block does while it runs.
DBMS_OUTPUT is an Oracle package that sends text from PL/SQL to a client tool. Its most common procedure is DBMS_OUTPUT.PUT_LINE, which writes one line of text.
Enabling server output
Your client must request DBMS_OUTPUT text before the text becomes visible. In SQL*Plus and SQLcl, use:
SET SERVEROUTPUT ONIn SQL Developer, enable the DBMS Output panel and the connection that runs the script. The exact controls differ between tools, but the PL/SQL code is the same.
The following anonymous block declares a message, then prints it:
SET SERVEROUTPUT ON
DECLARE
v_message VARCHAR2(50) := 'Hello, PL/SQL';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_message);
END;
/The slash runs the completed anonymous block in SQL*Plus-style clients. It is a client command, not part of the PL/SQL language.
A typical output line is:
Hello, PL/SQLThe output is buffered while the block runs. Your client usually displays it after the block finishes. DBMS_OUTPUT does not return rows like a SELECT statement, and it does not provide input to the block.
Building useful output
PUT_LINE accepts one character expression. Use the concatenation operator, ||, to combine labels and values:
SET SERVEROUTPUT ON
DECLARE
c_tax_rate CONSTANT NUMBER(4,3) := 0.075;
v_price NUMBER(10,2) := 100;
v_total NUMBER(10,2);
BEGIN
v_total := v_price * (1 + c_tax_rate);
DBMS_OUTPUT.PUT_LINE('Price: ' || TO_CHAR(v_price, 'FM9990.00'));
DBMS_OUTPUT.PUT_LINE('Tax rate: ' || TO_CHAR(c_tax_rate * 100, 'FM990.0') || '%');
DBMS_OUTPUT.PUT_LINE('Total: ' || TO_CHAR(v_total, 'FM9990.00'));
END;
/TO_CHAR converts a number to text using a format model. Explicit conversion makes output more predictable than relying on an implicit conversion, whose appearance can depend on session settings.
A null value needs care. In a concatenation expression, a null character value contributes no visible characters. This can produce an incomplete message:
DBMS_OUTPUT.PUT_LINE('Department: ' || v_department);If v_department is null, the output is only Department: . Use NVL when you need a visible replacement:
DBMS_OUTPUT.PUT_LINE('Department: ' || NVL(v_department, '[none]'));PUT, PUT_LINE, and NEW_LINE
PUT_LINE writes text and ends the line. PUT writes text without ending the line. NEW_LINE ends the current line.
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT('Status: ');
DBMS_OUTPUT.PUT_LINE('complete');
END;
/This prints one line:
Status: completeUse PUT_LINE for most messages. It is easier to read and avoids accidentally joining unrelated messages on one line.
What the output path looks like
If you see no output, check that server output is enabled, the correct connection is selected, and the block actually reached the PUT_LINE statement. An exception before that statement can prevent the message from appearing.
Exercise
What does this block print, and why is SET SERVEROUTPUT ON needed? SET SERVEROUTPUT ON BEGIN DBMSOUTPUT.PUT('Rows: '); DBMSOUTPUT.PUT_LINE(12); END; /
Show answer
It prints Rows: 12 on one line. PUT writes the label without a line break, and PUT_LINE writes 12 and ends the line. SET SERVEROUTPUT ON tells a SQL*Plus-style client to retrieve and display the buffered DBMS_OUTPUT text.