Advantages PL/SQL: Procedural Language Data Manipulation
Advantages PL/SQL: Procedural Language Data Manipulation
PL/SQL have a great functionality to display multiple records from the multiple
tables at the same time.
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;
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.
declare
var_first_name varchar2(20);
begin
exception
end;
-- Exception
set serveroutput on
declare
var_fname varchar2(20);
begin
exception
-- Anchored DataType
declare
var_salary employees.salary%type;
var_first_name employees.first_name%type;
begin
end;
-- Constants in PL/SQL
declare
begin
dbms_output.put_line(v_pi);
end;
-- Default in PL/SQL
declare
begin
dbms_output.put_line(v_pi);
end;
-- NOT NULL in PL/SQL
declare
begin
dbms_output.put_line(v_pi);
end;
variable v_bind
-- Bind Variable doesnot need Exceution block to get executed. Initialiation of 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;
begin
:v_bind := 'kohli';
dbms_output.put_line(:v_bind);
end;
print :v_bind;
set autoprint on
-- In Oracle PL/SQL we have two types of conditional control statements which are
-- 1. IF THEN Demonstartion1
declare
begin
if v_num<10 then
dbms_output.put_line('Inside IF');
end if;
end;
-- 1. IF THEN Demonstartion2
declare
begin
end if;
end;
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;
-- 3. IF THEN ELSIF
declare
begin
if v_place='hyderabad' then
dbms_output.put_line('Telangana State');
dbms_output.put_line('Karnataka State');
dbms_output.put_line('Maharashtra State');
else
dbms_output.put_line('No Information');
end if;
end;
-- Loops in PL/SQL
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;
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);
if v_counter>=10 then
exit;
end if;
end loop;
end;
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;
-- While loop
declare
v_counter number:=0;
v_result number;
begin
v_counter:=v_counter+1;
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
end loop;
end;
v_counter number:=0;
v_result number;
v_test boolean:=true;
begin
v_counter:=v_counter+1;
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
if v_counter=10 then
v_test:=false;
end if;
end loop;
end;
declare
v_counter number;
v_result number;
begin
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
end loop;
end;
declare
v_counter number;
v_result number;
begin
v_result:=20*v_counter;
dbms_output.put_line('20'||'X'||v_counter||'='||v_result);
end loop;
end;