Please disable your adblock and script blockers to view this page

Search this blog

Wednesday 7 March 2018

PL/SQL Basic Syntax, Block Structure and Anonymous block


PL/SQL is highly structured language and it's program units are written in the block of code, In this tutorial, we'll learn about basic syntax and the block structure of PL/SQL

A piece of code that is organized in a properly defined sequence is called a Block. A PL/SQL Block consists of 3 parts

DECLARE
<<declaration >>
--Declare Variables,Constants, Cursors and all elements

BEGIN
<<executable statements>>
--SQL, PL/SQL Commands 

EXCEPTION
<<exception handling>>
--Code to handle the exception

END;


So this is the basic structure of a simple PL/SQL block and a block without a name is known as Anonymous Block
Anonymous block is the simplest block and it can not be saved in DB so mostly it is used for testing and learning purpose

Let's write first PL/SQL program/block to print a message in SQL Developer

BEGIN 
DBMS_OUTPUT.PUT_LINE('FIRST PL/SQL PROGRAM');
END;

This anonymous block produces the following output


Now see another block in that we'll declare some variables and perform some calculation

DECLARE 
--Declare Variables
a integer :=10;
b integer :=20;
c integer;
BEGIN
--Perform Calculation
c:=a+b;
dbms_output.put_line('Value of C is- '||c);
END;

and output is


So here we have finished the first step towards learning PL/SQL. I am writing these tutorials as my personal notes so there will be more examples than theory part

Cheers :) Happy Learning

No comments :

Post a Comment