PL/SQL Notes
PL/SQL Notes
DEFINITION
• PL/SQL is a block structured language. The
programs of PL/SQL are logical blocks that can
contain any number of nested sub-blocks. Pl/SQL
stands for "Procedural Language extension of SQL"
that is used in Oracle.
• Although PL/SQL is closely integrated with SQL
language, yet it adds some programming
constraints that are not available in SQL.
PL/SQL Functionalities
• For constant
• pi constant number := 3.141592654;
Example
• DECLARE
• a integer := 30;
• b integer := 40;
• c integer;
• f real;
• BEGIN
• c := a + b;
• dbms_output.put_line('Value of c: ' || c);
• f := 100.0/3.0;
• dbms_output.put_line('Value of f: ' || f);
• END;
• /
Variable Scope in PL/SQL:
• Local Variable: Local variables are the inner
block variables which are not accessible to
outer blocks.
• Global Variable: Global variables are declared
in outermost block.
Eg
• DECLARE
• -- Global variables
• num1 number := 95;
• num2 number := 85;
• BEGIN
• dbms_output.put_line('Outer Variable num1: ' || num1);
• dbms_output.put_line('Outer Variable num2: ' || num2);
• DECLARE
• -- Local variables
• num1 number := 195;
• num2 number := 185;
• BEGIN
• dbms_output.put_line('Inner Variable num1: ' || num1);
• dbms_output.put_line('Inner Variable num2: ' || num2);
• END;
• END;
• /
PL/SQL Literals
• Numeric Literals
• Character Literals
• String Literals
• BOOLEAN Literals
• Date and Time Literals
PL/SQL If
IF condition
• IF condition
THEN
• THEN {...statements to execute when condition is T
• Statement: {It is executed when conditi RUE...}
on is true} ELSE
• END IF; {...statements to execute when condition is F
ALSE...}
END IF;
IF condition1 IF condition1
THEN THEN
{...statements to execute when condition1 is TRUE...} {...statements to execute when condition1 is TRUE...}
ELSIF condition2 ELSIF condition2
THEN THEN
{...statements to execute when condition2 is TRUE...} {...statements to execute when condition2 is TRUE...}
END IF; ELSE
{...statements to execute when both condition1 and condit
ion2 are FALSE...}
END IF;
Eg
• DECLARE
• a number(3) := 500;
• BEGIN
• -- check the boolean condition using if statement
• IF( a < 20 ) THEN
• -- if condition is true then print the following
• dbms_output.put_line('a is less than 20 ' );
• ELSE
• dbms_output.put_line('a is not less than 20 ' );
• END IF;
• dbms_output.put_line('value of a is : ' || a);
• END;
PL/SQL Case Statement
• LOOP
• EXIT WHEN i>10;
• DBMS_OUTPUT.PUT_LINE(i);
• i := i+1;
• END LOOP;
• END;
PL/SQL While Loop
• In PL/SQL, GOTO statement makes you able to get an unconditional jump from the
GOTO to a specific executable statement label in the same subprogram of the PL/SQL
block.
• Here the label declaration which contains the label_name encapsulated within the
<< >> symbol and must be followed by at least one statement to execute.
• <<label_name>>
• Statement;
• ..
• ..
• .
• GOTO label_name;
• ..
• ..
Eg
• DECLARE
• a number(2) := 30;
• BEGIN
• <<loopstart>>
• -- while loop execution
• WHILE a < 50 LOOP
• dbms_output.put_line ('value of a: ' || a);
• a := a + 1;
• IF a = 35 THEN
• a := a + 1;
• GOTO loopstart;
• END IF;
• END LOOP;
• END;
Restriction on GOTO statement
To Execute
EXECUTE greetings;
(OR)
BEGIN
greetings;
END;
Procedure with in & Out
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number)
IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
Another Eg
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number)
IS
BEGIN
x := x * x;
END;
BEGIN
a:= 5;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
Procedure Creation Eg
Table Creation
create table user(id number(10) primary key,name varchar2(100));
Procedure Creation
create or replace procedure "INSERTUSER"
(id IN NUMBER,
name IN VARCHAR2)
is
begin
insert into user values(id,name);
end;
/
Calling the procedure
BEGIN
insertuser(101,'Rahul');
dbms_output.put_line('record inserted success
fully');
END;
/
Drop a procedure
• DROP PROCEDURE procedure_name;
PL/SQL Function
END;
/
• CREATE OR REPLACE FUNCTION welcome_msgJune ( p_name IN VARCHAR2) RETURN VARCHAR2
• IS
• BEGIN
• RETURN (‘Welcome ‘|| p_name);
• END;
• /
• DECLARE
• lv_msg VARCHAR2(250);
• BEGIN
• lv_msg := welcome_msg_func (‘NEC’); dbms_output.put_line(lv_msg);
• END;