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 (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.
declare
begin
diffrentname varchar(30):=”michel jackson”
dbms_output.put_line(diffrentname);
end;
/
differentname variable is not declared in declare section , so error results
good i can understand easily
declare
diffrentname varchar(30):=’michel jackson’;
begin
dbms_output.put_line(diffrentname);
end;
/