Sa0951a PL/SQL 1: Introduction: An Introduction To The Procedural Language in Oracle
Sa0951a PL/SQL 1: Introduction: An Introduction To The Procedural Language in Oracle
http://uadisq01.uad.ac.uk:5560/isqlplus/
Sa0951a
PL/SQL 1: Introduction
An introduction to the
procedural language
in Oracle
Contents
What is PL/SQL?
Purpose – what is it for?
Block structure
Anonymous blocks
Main features
– rules
– Variables and data types
– Loops, branching
Lots of examples
2 October 18
What is PL/SQL?
3 October 18
A very short introduction:
4 October 18
PL/SQL Blocks
5 October 18
Some PL/SQL rules
6 October 18
Block structure
8 October 18
Defining variables and constants
Variables
– Variable_name datatype;
– Variable_name datatype := expression or value;
– Variable_name datatype NOT NULL := expression or
value;
– Are set to NULL by default
Constants
– constant_name CONSTANT datatype := expression or
value;
9 October 18
Block 1_1
Example
DECLARE
v_surname varchar2(20);
v_salary number(9,2):=0;
v_tax number(9,2):=ROUND(v_salary*0.25,2);
v_snum number(4) NOT NULL :=0;
c_tax1 CONSTANT number(3,2) := 0.10;
c_tax2 CONSTANT number(3,2) := 0.23;
BEGIN
v_snum := v_salary - v_tax;
END;
/
11 October 18
Anchoring data types: %TYPE
Generally …….
This allows a variable or constant to take on
v_varname table.column%type;
the same
c_constname datatype
CONSTANT as an attribute:=
table.column%type already
expression;
defined in a table
DECLARE
v_surname personnel.SURNAME%TYPE;
v_bonus personnel.BONUS%TYPE;
BEGIN
………………..
12 October 18
Block 1_2
Displaying output with
DBMS_OUTPUT and PUT_LINE
SET SERVEROUTPUT ON
DECLARE
v_surname varchar2(20):= 'BROWN';
v_salary number(9,2) := 10000;
BEGIN
DBMS_OUTPUT.PUT_LINE('Print these details');
DBMS_OUTPUT.PUT_LINE('-------------------');
DBMS_OUTPUT.PUT_LINE(v_surname||' earns '||v_salary);
DBMS_OUTPUT.PUT_LINE('+++++++++++++++++++');
END;
/
PL/SQL procedure successfully completed.
13 October 18
The NULL statement
DECLARE
v_divname branch.divname%Type;
v_surname personnel.surname%Type := UPPER('&surname');
BEGIN
DBMS_OUTPUT.PUT_LINE('converted to: '||v_surname);
END;
/
15 October 18
block 1_3
DECLARE
v_surname varchar2(10);
v_N1 number=23.4567;
v_joindate date
v_incep date:="28-Jan-67";
v_N2 number:=SQRT(Round(v_N1/3.4,2));
v_maxbonus number(3,2):= 300.67;
v_stockout bolean:=false;
BEGIN
NULL; -- develop associated code later
END
/
16 October 18
SELECT ….. INTO ………
DECLARE
v_surname personnel.surname%type;
v_bonus personnel.bonus%type;
BEGIN
SELECT surname, bonus*1.15
INTO v_surname, v_bonus Note the single ; at end
of SQL code
FROM PERSONNEL
WHERE SNUM = 3200;
DBMS_OUTPUT.PUT_LINE(v_surname||' earns ' ||v_bonus);
END;
/
•Would output - “RAINES earns 575”
18 October 18
Fuller Example
DECLARE qty_on_hand NUMBER(6);
BEGIN Looks up quantity
SELECT quantity INTO qty_on_hand FROM inventory of golf clubs from
inventory table and
WHERE product = 'golf club'; assigns to variable
IF qty_on_hand > 0 THEN Checks > 0
19 October 18
LOOPS
3 types:
– For Loop
– While Loop
– Simple or Infinite Loop
20 October 18
FOR Loop
BEGIN TEST
FOR v_count IN 1..10 LOOP ID_NO
Insert into test(id_no) 1
values(v_count); 2
END LOOP; 3
END; 4
/ 5
6
7
8
Notes: 9
• v_count is NOT declared -- it is implicit 10
DECLARE TEST
V_count number(2):=1; ID_NO
BEGIN 1
WHILE LOOP 2
WHILE v_count < 11 LOOP 3
Insert into test(id_no) 4
values(v_count); 5
v_count:=v_count+1; 6
END LOOP; 7
8
END; 9
/ 10
22 October 18
Infinite Loop Example
DECLARE TEST
V_count number(2):=1; ID_NO
BEGIN 1
2
LOOP 3
Insert into test(id_no) 4
values(v_count); 5
EXIT WHEN v_count=10; 6
v_count:=v_count+1; 7
8
END LOOP; 9
END; 10
/
23 October 18
IF…THEN…ELSIF…ELSE
26 October 18
Some other useful examples…..
<<Labels>> (useful when nesting)
BEGIN
<<firstloop>>
FOR counter in 1..2 Loop
DBMS_OUTPUT.PUT_LINE('1st: '||firstloop.counter);
<<secondloop>>
FOR counter in 1..4 Loop
DBMS_OUTPUT.PUT_LINE('1st: '|| firstloop.counter);
DBMS_OUTPUT.PUT_LINE('2nd: '|| secondloop.counter);
END LOOP secondloop; -- aids readability
DBMS_OUTPUT.PUT_LINE('--------------------');
END LOOP firstloop; -- aids readability
END;
/
27 October 18
IF: Conditional Tests supported
Logicals:
– AND OR NOT
Expressions:
– IS [NOT] NULL, [NOT] BETWEEN a AND b
– [NOT] like a, [NOT] IN list
Comparisons: < > <= >= <> !=
Operations: + - * / (and more)
Functions: any legal SQL function
28 October 18
Reading & References
29 October 18