In my previous articles, I have explained about different oracle tutorials, various interview questions and BI tutorials as well. In this article, I will try to explain about the advanced database language, which is PL/SQL. In this article, we are Starting with PL/SQL which stands for Procedural Language- Structured Query language. Oracle has developed the advanced SQL language to do the transactions in the database. PL SQL is nothing but extension to the standard SQL with newly added transactional language features. In this article, I will try to give the basic idea of Anonymous block in PL/SQL and how to write Anonymous block in PL SQL.
Why PL/SQL?
The PL/SQL language is extension to SQL language. There are lot of limitations while working with single SQL statement. Lot of times as per business requirements user needs to execute the set of SQL statements. There is need to execute the procedural logic in SQL statement. For these reasons oracle has defined the procedural language with additional procedural features:
PL SQL provides the mechanism for developers to add the procedural component at server level. Means PL SQL is used to add the complex business logic, business rules in database language.
Let us start with the Anonymous block in PL/SQL. The PL/SQL language has a block structure. The block structure should contain set of SQL statements, Set of Exceptions, Error handlers, functions, variable declaration etc.
Syntax of PL SQL Block:
DECLARE
variable_declarations
BEGIN
program_code
EXCEPTION
exception_handlers
END;
The above is the simple block structure of PL/SQL code. Every user will start with how to write the PL/SQL block. This is universal block structure of PL/SQL. In this article I will try to give you the idea of writing simple PL/SQL code using the block structure. The block structure is converted in to 3 parts :
In Declarative section all the variable declaration as well as function declaration is defined.
Example:
Declare
Variable1 Number (10, 2);
In between Begin and End the actual program, code is written. The business logic needs to be added in Begin section. The set of SQL statement has been written in begin section. User will be able to add the looping as well as if. Else structure in begin section.
Example:
Declare
Variable1 Number (10, 2);
Begin
Select 6/2 into Variable1 from dual;
Dbms_output.Put_line (‘The Division of 6/2 is’||Variable1);
End;
After Begin, section user needs to deal with the errors and exceptions. The Oracle has given the provision to handle exceptions. The exceptions are written in exception handling block. There are two types of exceptions in PL/SQL. One is system exceptions and other is user defined exceptions. The exceptions block is non mandatory block.
Example :
Declare
Variable1 Number (10, 2);
Begin
Select 6/2 into Variable1 from dual;
Dbms_output.Put_line (‘The Division of 6/2 is’||Variable1);
Exception when divide_by_zero then
Dbms_output.Put_line (‘Number can not be divided by zero’);
End;
I have explained the simple PL SQL block in above example.
Things to Remember:
When you are defining PL/SQL functions, procedures, and triggers, the keyword DECLARE is not used. When defining a function, the function specification, or function header as it is sometimes called, begins the block. Means everything, which has not started with Declare keyword is known as PL SQL objects.
The following are steps to run the PL SQL program using SQL developer. The user can run the PL SQL program using following two types:
User can run the PL SQL program using direct server. There are following steps.
Step 1 :
Connect with SQL* PLUS :
Step 2 : Use backslash to Run Program
Just write the program and put backslash before running it :
declare
x integer;
begin
x := 111;
dbms_output.Put_line(x);
end;
/ –Backslash is important to run program-using SQLPLUS
Output : PL/SQL procedure successfully completed
Using this the output of procedure is not displaying.So you need to set server output as On.
Step 3: Set serveroutput On
User need to set Server output as On to display the output of any procedure.
SQL> SET SERVEROUTPUT ON
Step 4: Run program again with backslash
declare
x integer;
begin
x := 111;
dbms_output.Put_line(x);
end;
/ –Backslash is important to run program-using SQLPLUS
Output : 3
User can run the PL SQL block in very simple steps using Editor.I will explain how to run PL SQL using SQL developer in this section.
Step 1 :
Connect with SQL developer
User needs to open the SQL developer and click on + sign on SQL developer.
Step 2 :
Put all required server credentials and connect with SQL developer.
Step 3 :
Write the code and Run it using key F9 or use the green arrow to run the program.
These are two different processes used to run the simple PLSQL block. Hope everyone get the idea of anonymous block in PL/SQL. If you really like this article or if you have any suggestions related to this article of Anonymous block in PL/SQL kindly comment in comment section.
In my previous article I have given details about application support engineer day to day…
In my previous articles I have given the roles and responsibilities of L1,L2 and L3…
In my previous articles i have given the hierarchy of production support in real company…
In this article i would like to provide information about production support organization structure or…
In my previous article I have given roles for L1 and L2 support engineer with…
I have started this new series of how to become application support engineer. This article…