Every programming journey starts with a "Hello World" program. It is the traditional way to test the environment, confirm that code compiles, and understand the basic structure of a programming language. In Oracle Database 23ai, developers use PL/SQL for procedural programming within the database.
Writing a "Hello World" program in PL/SQL is the best way to get familiar with the block structure, syntax, and output mechanism of the language. This Oracle tutorial will guide you step by step through writing and executing your first "Hello World" program in Oracle Database 23ai.
Structure of a PL/SQL Block
In Oracle, PL/SQL code is organized into blocks. Each block has three sections—declaration, execution, and exception handling. Only the execution section is mandatory.
DECLARE -- variable declarations (optional) BEGIN -- executable statements (mandatory) EXCEPTION -- exception handlers (optional) END; /
- DECLARE: Used for defining variables, cursors, or constants.
- BEGIN...END: Contains the main logic of the program.
- EXCEPTION: Handles runtime errors gracefully.
For a simple "Hello World", only the BEGIN...END section is needed.
Basic Hello World Program
The simplest way to display "Hello World" in Oracle PL/SQL uses the DBMS_OUTPUT.PUT_LINE procedure.
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World from Oracle Database 23ai!');
END;
/
Result:
Hello World from Oracle Database 23ai!
This confirms that PL/SQL code compiles and executes successfully.
Enabling Output in SQL*Plus or SQL Developer
By default, output may not appear until you enable server output:
SET SERVEROUTPUT ON;
Without this, you may execute the block successfully but see no text.
Hello World with Variables
You can declare a variable and print its value. This demonstrates the use of the DECLARE section.
DECLARE v_message VARCHAR2(50) := 'Hello World using a variable'; BEGIN DBMS_OUTPUT.PUT_LINE(v_message); END; /
Result:
Hello World using a variable
This is useful when working with dynamic messages or concatenated strings.
Hello World with String Concatenation
Oracle PL/SQL uses the double pipe operator (||) for concatenation.
DECLARE
v_name VARCHAR2(30) := 'Oracle Developer';
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World, ' || v_name || '!');
END;
/
Result:
Hello World, Oracle Developer!
This shows how PL/SQL can create dynamic strings.
Hello World with Exception Handling
Adding exception handling makes the block more robust.
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World with error handling');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred while printing message');
END;
/
Even though no error occurs here, this pattern is standard for production PL/SQL programs.
Hello World with JSON in Oracle 23ai
Oracle 23ai has enhanced JSON support. You can integrate a JSON object in your Hello World example.
DECLARE
v_data JSON_OBJECT_T;
BEGIN
v_data := JSON_OBJECT_T('{"message":"Hello World from JSON in 23ai"}');
DBMS_OUTPUT.PUT_LINE(v_data.get_String('message'));
END;
/
Result:
Hello World from JSON in 23ai
This combines PL/SQL basics with modern Oracle features.
A More Practical Hello World Example
Sometimes developers create a "Hello World" procedure that can be reused.
CREATE OR REPLACE PROCEDURE hello_world_proc IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World Procedure in Oracle 23ai');
END;
/
Now, you can call it:
BEGIN hello_world_proc; END; /
Result:
Hello World Procedure in Oracle 23ai
This teaches how to define and invoke stored procedures.
Conclusion
The "Hello World" program in Oracle Database 23ai PL/SQL is more than just a tradition—it is the first step toward mastering PL/SQL. You learned how to print messages, use variables, concatenate strings, handle exceptions, and even integrate JSON features. These foundational concepts will help you build complex business logic, stored procedures, and AI-powered applications in Oracle 23ai.
