0% found this document useful (0 votes)
7 views81 pages

PL SQL

The document provides an introduction to PL/SQL, highlighting its differences from SQL, including network efficiency and block structures. It covers the types of PL/SQL blocks, variable declarations, data types, control structures, loops, and exception handling. Additionally, it explains how to manipulate character strings and use SQL queries within PL/SQL programs.

Uploaded by

Pallavi Digra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views81 pages

PL SQL

The document provides an introduction to PL/SQL, highlighting its differences from SQL, including network efficiency and block structures. It covers the types of PL/SQL blocks, variable declarations, data types, control structures, loops, and exception handling. Additionally, it explains how to manipulate character strings and use SQL queries within PL/SQL programs.

Uploaded by

Pallavi Digra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 81

Introduction to PL/SQL

1
2
DIFFERENCE BETWEEN PL/SQL AND SQL
• When a SQL statement is issued on the client computer, the request
is made to the database on the server, and the result set is sent
back to the client.

• As a result, a single SQL statement causes two trips on the network.


If multiple SELECT statements are issued, the network traffic
increase significantly very fast. For example, four SELECT
statements cause eight network trips.

• If these statements are part of the PL/SQL block, they are sent to the
server as a single unit. The SQL statements in this PL/SQL program
are executed at the server and the result set is sent back as a single
unit. There is still only one network trip made as is in case of a single
SELECT statement.

3
PL/SQL BLOCKS

• PL/SQL blocks can be divided into two groups:


1. Named and
2. Anonymous.

• Named blocks are used when creating subroutines. These


subroutines are procedures, functions, and packages.

• The subroutines can be stored in the database and referenced by


their names later on.

• In addition, subroutines can be defined within the anonymous


PL/SQL block.

• Anonymous PL/SQL blocks do not have names. As a result,they


cannot be stored in the database and referenced later.
4
PL/SQL BLOCK STRUCTURE

• PL/SQL blocks contain three sections


1. Declare section
2. Executable section and
3. Exception-handling section.

• The executable section is the only mandatory section of the block.

• Both the declaration and exception-handling sections are optional.

5
PL/SQL Program Blocks

• Comments:
– Not executed by interpreter
– Enclosed between /* and */
– On one line beginning with --

6
Fundamentals of PL/SQL
• Full-featured programming language
• An interpreted language
• Type in editor, execute in SQL*Plus

7
Variables and Data Types

• Variables
– Used to store numbers, character strings, dates,
and other data values
– Avoid using keywords, table names and column
names as variable names
– Must be declared with data type before use:
variable_name data_type_declaration;

8
Scalar Data Types
• Represent a single value

9
Composite and Reference Variables

• Composite variables
– RECORD: contains multiple scalar values, similar to a table
record
– TABLE: tabular structure with multiple columns and rows
– VARRAY: variable-sized array
• Reference variables
– Directly reference a specific database field or record and
assume the data type of the associated field or record
– %TYPE: same data type as a database field
– %ROWTYPE: same data type as a database record

10
Variables and Types
• %TYPE operator
DECLARE
myEmpNo EMP.empno%TYPE;
• %ROWTYPE operator
DECLARE
EmpTuple Emp%ROWTYPE;

11
Arithmetic Operators

12
Assignment Statements

• Assigns a value to a variable


• variable_name := value;
• Value can be a literal:
– current_s_first_name := 'John';
• Value can be another variable:
– current_s_first_name := s_first_name;

13
Executing a PL/SQL Program in
SQL*Plus

• Create program in text editor


• Paste into SQL*Plus window
• Press Enter, type / then enter to execute
14
Manipulating Character Strings with
PL/SQL
• To concatenate two strings in PL/SQL, you use the
double bar (||) operator:
– new_string := string1 || string2;

• To remove blank leading spaces use the LTRIM


function:
– string := LTRIM(string_variable_name);

• To remove blank trailing spaces use the RTRIM


function:
– string := RTRIM(string_variable_name);

• To find the number of characters in a character string


use the LENGTH function:
– string_length := LENGTH(string_variable_name);

15
Manipulating Character Strings with
PL/SQL
• To change case, use UPPER, LOWER, INITCAP
• INSTR function searches a string for a specific substring:
– start_position := INSTR(original_string, substring);

• SUBSTR function extracts a specific number of characters


from a character string, starting at a given point:
– extracted_string := SUBSTR(string_variable, starting_point,
number_of_characters);

16
PL/SQL Decision Control
Structures
• Use IF/THEN structure to execute code if condition is true
– IF condition THEN
commands that execute if condition is TRUE;
END IF;
• If condition evaluates to NULL it is considered false
• Use IF/THEN/ELSE to execute code if condition is true or
false
– IF condition THEN
commands that execute if condition is TRUE;
ELSE
commands that execute if condition is FALSE;
END IF;
• Can be nested – be sure to end nested statements
17
PL/SQL Decision Control
Structures
• Use IF/ELSIF to evaluate many conditions:
– IF condition1 THEN
commands that execute if condition1 is TRUE;
ELSIF condition2 THEN
commands that execute if condition2 is TRUE;
ELSIF condition3 THEN
commands that execute if condition3 is TRUE;
...
ELSE
commands that execute if none of the
conditions are TRUE;
END IF;
18
IF/ELSIF Example

19
Complex Conditions
• Created with logical operators AND, OR and NOT
• AND is evaluated before OR
• Use () to set precedence

20
Using SQL Queries in PL/SQL
Programs
• Action queries can be used as in SQL*Plus

• May use variables in action queries

• DDL commands may not be used in PL/SQL

21
Loops
• Program structure that executes a series of program
statements, and periodically evaluates an exit
condition to determine if the loop should repeat or
exit
• Pretest loop: evaluates the exit condition before any
program commands execute
• Posttest loop: executes one or more program
commands before the loop evaluates the exit
condition for the first time
• PL/SQL has 5 loop structures

22
23
24
DECLARE
x NUMBER := 100;
BEGIN
FOR i IN 1..10 LOOP
IF MOD(i,2) = 0 THEN -- i is even
dbms_output.put_line( x || ‘i is even');
ELSE
dbms_output.put_line( x || ‘i is odd');
END IF;

x := x + 100;
END LOOP;
COMMIT;
END;
.
run

25
DECLARE
salary emp.sal%TYPE;
mgr_num emp.mgr%TYPE;
last_name emp.ename%TYPE;
starting_empno CONSTANT NUMBER(4) := 7902;
BEGIN
SELECT sal, mgr INTO :salary, :mgr_num FROM emp
WHERE empno = :starting_empno;
WHILE salary < 4000 LOOP
SELECT sal, mgr, ename INTO :salary, :mgr_num, :last_name
FROM emp
WHERE empno = :mgr_num;
END LOOP;

INSERT INTO temp VALUES (NULL, :salary, :last_name);


COMMIT;
END;

26
EXCEPTION-HANDLING SECTION
• The exception-handling section is the last section of the PL/SQL block.
• This section contains statements that are executed when a runtime error
occurs within a block.
• Runtime errors occur while the program is running and cannot be detected
by the PL/SQL compiler.

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
(‘ There is no student with student id 123 ’);
END;

27
28
29
30
31
32
33
34
35
Handling Runtime Errors in PL/SQL
Programs
• Runtime errors cause exceptions
• Exception handlers exist to deal with different error situations
• Exceptions cause program control to fall to exception section
where exception is handled

36
Predefined Exceptions

37
Undefined Exceptions

• Less common errors


• Do not have predefined names
• Must declare your own name for the
exception code in the declaration section
– DECLARE
e_exception_name EXCEPTION;
PRAGMA
EXCEPTION_INIT(e_exception_name,
-Oracle_error_code);
38
User-Defined Exceptions

• Not a real Oracle error


• Use to enforce business rules

39
40
40
41
41
42
42
43
43
44
44
45
45
46
46
47
47
48
48
49
49
50
50
51
51
52
52
53
53
54
54
55
55
56
56
57
57
58
58
59
59
60
60
61
61
62
62
63
63
64
64
65
65
66
66
67
67
68
68
69
69
70
70
71
71
72
72
73
73
74
74
75
75
76
76
77
77
78
78
79
79
80
80
81
81

You might also like