PLSQL 1 3 SG
PLSQL 1 3 SG
Updates:
1
2
3
4
5
Students will learn about nested blocks later in the course.
6
DECLARE is not needed if no variables, constants, cursors or user-defined exceptions are required. But
nearly all real-life blocks will need variables and/or cursors, therefore nearly all real-life blocks will need a
DECLARE section.
7
Students will learn later that we can recompile stored subprograms (procedures, functions and packages)
on demand by (for example): ALTER PROCEDURE procedure_name COMPILE;
8
9
10
11
DBMS_OUTPUT.PUT_LINE is explained at the end of this lesson.
12
If students ask “what is TOO_MANY_ROWS ?”, tell them it means that a SELECT statement fetched more
than one row from the database; in this example, it would mean that the COUNTRIES table contains at least
two rows whose country_id = 'CA'.
13
Every language has a syntax, a vocabulary, and a character set. You have to learn the rules that govern its usage. PL/SQL is made up of many
BLOCKS of code. A PL/SQL block has up to four sections, only one is mandatory.
Header - optional
Declaration section – DECLARE identifiers - optional
Executable section – BEGIN…END; - mandatory
Exception - Error Handling – optional
Named Program:
PROCEDURE name (Header, Optional)
IS
DECLARE (Optional)
Variables, cursors, user- defined exceptions
BEGIN ( Executable, start, Mandatory)
- SQL statements
- PL/SQL statements
EXCEPTION (Optional)
Actions to perform when errors occur
END; (Executable, end, Mandatory)
Anonymous Block with a declaration section:
/*Declaration section program*/
DECLARE
words varchar2(15) := ‘Hello World’;
BEGIN
DBMS_OUTPUT.PUT_LINE(words);
END;
Anonymous Block without a declaration section:
/*No Declaration section program*/
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Hello’ || ‘ World’);
END;
14
15
No instructor notes for this slide. Note that this slide is providing an example of a procedure and a function
merely for you to compare and contrast, not necessarily for you or the students to execute.
16
17
18
19
20
The slide shows an example of a PL/SQL anonymous block in SQL Commands. The following is an example
of a SQL Script, containing two SQL statements and one anonymous PL/SQL block. Note that in a script,
anonymous PL/SQL blocks must be followed by a forward slash (/). This is not needed in SQL Commands
(although it can be used).
21
22
23
The second call in the slide shows that number values (v_emp_count) can be displayed by DBMS_OUTPUT.
In this example, the Oracle server has performed an implicit datatype conversion
(TO_CHAR(v_emp_count)) to convert the number to a character string for concatenation.
24
Anonymous PL/SQL block – unnamed blocks of code not stored in the database and do not exist after they
are executed
Compiler – software that checks and translates programs written in high-level programming languages into
binary code to execute
Subprograms – named PL/SQL blocks that are stored in the database and can be declared as procedures or
functions
Procedures – programs that perform an action and may return values
Functions – programs that compute and return a single value
25
26
27