Hello World Program in PL/SQL

So, now that you know something very basic about PL/SQL programming language, let’s get our hands dirty into coding. Lets start with the very first program for printing hello world to get the basic cleared.

Open your SQL terminal and first of all type.

Set serveroutput on;

Type the above command before executing any SQL program.

Note: PL/SQL is not case sensitive, so you can write the code in any case.

Hello World Program in PL/SQL

Hello World Program in PL/SQL (Type 1) 

begin
dbms_output.put_line('Hello World');
end;
/

Type the above code in your SQL terminal as it is and hit Enter.

Output
Hello World
PL/SQL procedure successfully completed.

The above program contains a Begin block which is of utmost importance in most of the PL/SQL procedures. This block contains the BEGIN keyword at the first line.

dbms_output.put_line(): It is used for printing the output to the console screen. It is due to output activation that we write ‘set serveroutput on’ at start of the program. dbms_output.put_line() takes a parameter which is directly printed onto the console screen. There are many variations in the parameters which we shall look in detail in the subsequent tutorials.

Note: Here we haven’t used Declaration section and Exception section as we don’t need any of them for this program and both of these sections are optional. We have only used Execution section here.

Finally as we have written our main task in the execution section to print the output, now we have to write ‘end’ to indicate the SQL engine that the Execution section is now completed. After you have finished writing the code just press Enter and the output will be automatically printed on the console.
 

Important Things to Remember

  • The executable portion of a PL/SQL program block starts with the keyword ‘Begin’ and is terminated with the keyword ‘End’.
  • Each statement ends with a semi-colon.
  • PL/SQL code blocks are followed by a slash (/) in the first position of the following line. This causes the code block statements to be executed.

The other way to get the Hello World program executed in a different way is as follows.

Hello World Program in PL/SQL (Type 2) 

DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/

Output
Hello World
PL/SQL procedure successfully completed.

Here we have two sections in the block of PL/SQL code.
 
1. Declaration section
2. Execution section
 
Declaration section contains a variable which is declared by the programmer. The variable is ‘message’ and the datatype is varchar2. The dimension is given in the parameter of the datatype. varchar2 is an extended version of varchar.

The Execution section starts with the BEGIN keyword and we have given the variable declared in the Declaration section set as the parameter to the output statement. Finally, we now write END keyword to terminate the block of the Execution section.

4 thoughts on “Hello World Program in PL/SQL”

Leave a Comment

Your email address will not be published. Required fields are marked *