What is PL/SQL
PL/SQL is a Procedural Language extension of Structured Query Language (SQL).
PL/SQL is specially designed for Database oriented activities. Oracle PL/SQL allows you to
perform data manipulation operation those are safe and flexible.
PL/SQL is a very secure functionality tool for manipulating, controlling, validating,
and restricting unauthorized access data from the SQL database.
Using PL/SQL we can improve application performance. It also allows
to deal with errors so we can provide user friendly error messages.
PL/SQL have a great functionality to display multiple records from the multiple
tables at the same time.
PL/SQL is capable to send entire block of statements and execute it in the Oracle
engine at once.
Advantages PL/SQL
Procedural language support : PL/SQL is a development tools not
only for data manipulation futures but also provide the conditional
checking, looping or branching operations same as like other
programming language.
Reduces network traffic : This one is great advantages of PL/SQL.
Because PL/SQL nature is entire block of SQL statements
execute into oracle engine all at once so it's main benefit
is reducing the network traffic.
Error handling : PL/SQL is dealing with error handling, It's permits
the smart way handling the errors and giving user friendly error
messages, when the errors are encountered.
Declare variable : PL/SQL gives you control to declare
variables and access them within the block. The declared
variables can be used at the time of query processing.
Intermediate Calculation : Calculations in PL/SQL done quickly and
efficiently without using Oracle engines. This improves the
transaction performance.
Portable application : Applications are written in PL/SQL
are portable in any Operating system. PL/SQL applications
are independence program to run any computer.
PL/SQL is block structured language divided into three logical
blocks.
BEGIN block and END; keyword are compulsory, and other two
block DECLARE and EXCEPTION are optional block. END; is not
a block only keyword to end of PL/SQL program.
PL/SQL block structure follows divide-and-conquer approach to
solve the problem stepwise.
Declare Block
Variables and constants are declared, initialized within this section.
Variables and Constants : In this block, declare and initialize variables
(and constants). You must have to declare variables and constants in
declarative block before referencing them in procedural statement.
Declare Variables and Assigning values : You can define variable name,
data type of a variable and its size. Date type can be: CHAR,
VARCHAR2, DATE, NUMBER, INT or any other.
Begin Block
BEGIN block is procedural statement block which will implement the
actual programming logic. This section contains conditional statements
(if...else), looping statements (for, while) and Branching Statements
(goto) etc.
Exception Block
PL/SQL easily detects user defined or predefined error condition.
PL/SQL is famous for handling errors in smart way by giving suitable
user friendly messages. Errors can be rise due to wrong syntax, bad
logical or not passing a validation rules
PL/SQL Programming
-- First PL/SQL Program
set SERVEROUTPUT ON
declare
var_first_name varchar2(30):='virat'; -- Declartion and Initialization of the variable at the Declare Block
begin
DBMS_OUTPUT.put_line(var_first_name);
end;
-- Second PL/SQL Program
declare
var_first_name varchar2(30); -- Declartion at Declare Block and Initialization at the Execution Block
begin
var_first_name:='virat';
DBMS_OUTPUT.put_line(var_first_name);
end;
-- Fetching a value from the column of a row of a table and assigning that value to PL/SQL Variable.
-- This requirement is possible by using SELECT INTO Statement
declare
var_first_name varchar2(20);
begin
select first_name into var_first_name from employees where employee_id=100;
dbms_output.put_line('Fetched Value from Employee Table: '||var_first_name);
exception
when no_data_found then
dbms_output.put_line('Table Doesnot Contain Rows');
end;
-- select * from employees where employee_id=100;
-- Exception
--create table employ(eid number,fname varchar2(10));
--select * from employ;
set serveroutput on
declare
var_fname varchar2(20);
begin
select fname into var_fname from employ;
dbms_output.put_line('Fetched Value from Employee Table: '||var_fname);
exception
when no_data_found then
dbms_output.put_line('Table Doesnot Contain Rows');
end;
-- select fname from employ;
-- Anchored DataType
declare
var_salary employees.salary%type;
var_first_name employees.first_name%type;
begin
select first_name,salary into var_first_name,var_salary from employees where employee_id=100;
dbms_output.put_line(var_first_name||' Earns Monthly Salary: '||var_salary||'$');
end;
-- Constants in PL/SQL
declare
v_pi constant number(8,7):=3.1415921;
begin
dbms_output.put_line(v_pi);
end;
-- Default in PL/SQL
declare
v_pi constant number(8,7) default 3.1415921;
begin
dbms_output.put_line(v_pi);
end;
-- NOT NULL in PL/SQL
declare
v_pi constant number(8,7) not null default 3.1415921;
begin
dbms_output.put_line(v_pi);
end;
-- Declation of Bind Variable
variable v_bind varchar2(20);
-- To See the Definition of Bind Variable
variable v_bind
-- Bind Variable doesnot need Exceution block to get executed. Initialiation of Bind Variable
exec :v_bind :='viart';
-- Printing Bind Variable
-- Initialization of the bind variable by explicitly writing execution section of PL/SQL block
begin
:v_bind := 'kohli';
dbms_output.put_line(:v_bind);
end;
-- Displaying Bind Variable Value (3 Ways) 1.dbms_output.put_line 2.Print 3. Setting Autoprint
parameter on
-- 1.dbms_output.put_line
begin
:v_bind := 'kohli';
dbms_output.put_line(:v_bind);
end;
-- 2. Print Command to display bind variable value
print :v_bind;
-- 3. Setting Autoprint parameter on
set autoprint on
--set autoprint off
variable v_bind varchar2(20);
exec :v_bind :='viart';
-- In Oracle PL/SQL we have two types of conditional control statements which are
-- 1. IF statements (1. IF THEN 2. IF THEN ELSE 3. IF THEN ELSIF)
-- 2. CASE statements (1. SIMPLE CASE and 2. SEARCHED CASE)
-- 1. IF THEN Demonstartion1
declare
v_num number :=9;
begin
if v_num<10 then
dbms_output.put_line('Inside IF');
end if;
end;
-- 1. IF THEN Demonstartion2
declare
v_name varchar2(20) :='virat';
v_prof varchar2(20) :='cricketer';
begin
if v_name='virat' and v_prof='cricketer' then
dbms_output.put_line('Yes, You are Right');
end if;
dbms_output.put_line('IF THEN BLOCK COMPLETES');
end;
-- 2. IF THEN ELSE (Even or Odd Number)
declare
v_num number:='&Enter_a_Number';
begin
if mod(v_num,2)=0 then
dbms_output.put_line(v_num||' is Even');
else
dbms_output.put_line(v_num||' is Odd');
end if;
dbms_output.put_line('IF THEN ELSE BLOCK COMPLETES');
end;
-- 3. IF THEN ELSIF
declare
v_place varchar2(20):= '&Enter_the_City_in_INDIA';
begin
if v_place='hyderabad' then
dbms_output.put_line('Telangana State');
elsif v_place='bangalore' then
dbms_output.put_line('Karnataka State');
elsif v_place='mumbai' then
dbms_output.put_line('Maharashtra State');
else
dbms_output.put_line('No Information');
end if;
dbms_output.put_line('IF THEN ELSIF Completes');
end;
-- Loops in PL/SQL
-- Loop without Exit
declare
v_counter number:=0;
v_result number;
begin
loop
v_counter:=v_counter+1;
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
end loop;
end;
-- Loop with Exit clause
declare
v_counter number:=0;
v_result number;
begin
loop
v_counter:=v_counter+1;
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
-- Loop Termination Code Starts
if v_counter>=10 then
exit;
end if;
-- Loop Termination Code Ends
end loop;
end;
-- Loop with Exit when clause
declare
v_counter number:=0;
v_result number;
begin
loop
v_counter:=v_counter+1;
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
exit when v_counter>=10; -- Loop Termination Code
end loop;
end;
-- While loop
declare
v_counter number:=0;
v_result number;
begin
while v_counter<10 loop
v_counter:=v_counter+1;
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
end loop;
end;
-- While Loop (Boolean Expression as Test Condition)
declare
v_counter number:=0;
v_result number;
v_test boolean:=true;
begin
while v_test loop
v_counter:=v_counter+1;
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
-- Loop Termination Code Starts
if v_counter=10 then
v_test:=false;
end if;
-- Loop Termination Code Ends
end loop;
end;
-- Numeric Loops (For Loop)
declare
v_counter number;
v_result number;
begin
for v_counter in 1..10 loop
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
end loop;
end;
-- For Loop in Reverse
declare
v_counter number;
v_result number;
begin
for v_counter in reverse 1..10 loop
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
end loop;
end;