PL/SQL SET Serveroutput ON

Understanding SET SERVEROUTPUT ON in PL/SQL: Why It Matters and How to Use It

When working with Oracle PL/SQL, one of the first and most essential commands you'll need to know is:

SET SERVEROUTPUT ON;

This simple line plays a crucial role in how your PL/SQL programs behave — especially when you want to display output from procedures, blocks, or scripts using DBMS_OUTPUT.PUT_LINE.

Why Do We Use SET SERVEROUTPUT ON?

By default, PL/SQL code runs silently inside the Oracle database engine. That means even if your program generates output, you won’t see any result on your screen — unless you explicitly enable server output.

This is where the command SET SERVEROUTPUT ON comes into play. It instructs SQL*Plus or SQL Developer to display the output from the DBMS_OUTPUT buffer, allowing you to see the result of your program on the screen.

Syntax

SET SERVEROUTPUT ON;

You can also control the buffer size and format (optional):

SET SERVEROUTPUT ON SIZE 1000000 FORMAT WRAPPED;

When Should You Use It?

  • Whenever you're using DBMS_OUTPUT.PUT_LINE to display output.

  • During testing and debugging of PL/SQL procedures or anonymous blocks.

  • To trace variable values or track the flow of control.

Example: Using SET SERVEROUTPUT ON in a PL/SQL Block

Let's look at a simple PL/SQL anonymous block that declares variables and uses DBMS_OUTPUT.PUT_LINE to print them:

SET SERVEROUTPUT ON;

DECLARE
    eno NUMBER(5) NOT NULL := 2;
    ename VARCHAR2(15) := 'Branson Devs';
    edept CONSTANT VARCHAR2(15) := 'Web Developer';
BEGIN
    dbms_output.put_line('Declared Value:');
    dbms_output.put_line(' Employee Number: ' || eno || ' Employee Name: ' || ename);
    dbms_output.put_line('Constant Declared:');
    dbms_output.put_line(' Employee Department: ' || edept);
END;
/

Output (Only Visible If SERVEROUTPUT Is ON)

Declared Value:
Employee Number: 2 Employee Name: Branson Devs
Constant Declared:
Employee Department: Web Developer

Important

If you omit the SET SERVEROUTPUT ON command, the DBMS_OUTPUT.PUT_LINE results will not be displayed, even though the block executes successfully.

Tips

  • In SQL Developer, go to View > DBMS Output, then click the green + icon to enable output for your session.

  • In SQL*Plus, always run SET SERVEROUTPUT ON before any PL/SQL block using output.

  • Use SET SERVEROUTPUT OFF when you no longer need output to be displayed.

Conclusion

The SET SERVEROUTPUT ON command is a small but vital part of writing and debugging PL/SQL code. It enables visibility into your PL/SQL logic by allowing output messages to be displayed on screen — making your development workflow smoother and more transparent.