Saturday, 24 March 2012

Your first simple Pl/SQL program...

How to write your first PL/SQL program. Writing a PL/SQL program is not difficult. This post deals with writing a simple anonymous PL/SQL block. Let me remind you, anonymous block is a block without any name, i.e. you cannot call an anonymous by name. You can only execute an anonymous block while writing it, it cannot be stored and executed later.

The program written below assigns a string to a variable and prints it.

Before writing your PL/SQL block set your serveroutput on in Sql plus. We set serveroutput on, so that we could see output on sql plus.

SET SERVEROUTPUT ON

Now we will write the block/program.


DECLARE
v_name varchar2(20);
BEGIN
v_name:='John';
DBMS_OUTPUT.PUT_LINE('My name is '||v_name);
END;
/


Above block is divided into 3 sections, i.e.

DECLARE: We have declared a variable "v_name" in this section.


BEGIN: In this section we assign a value to v_name and later print it using PUT_LINE procedure of DBMS_OUTPUT package. "||" is the concatenation operator as we have "+'' in java or "." in PHP. Also notice the line of code after BEGIN, we use ":=" for assignment in oracle instead of "=".


END: END is used to end the block and after that "/" is used to execute the block.

If everything goes right, you will see the output "John" on Sql plus.

No comments:

Post a Comment