PLSQL 1 3
PLSQL 1 3
PLSQL 1 3
PL/SQL
1-3
Creating PL/SQL Blocks
Objectives
This lesson covers the following objectives:
Describe the structure of a PL/SQL block
Identify the different types of PL/SQL blocks
Identify PL/SQL programming environments
Create and execute an anonymous PL/SQL block
Output messages in PL/SQL
PLSQL1-3
Creating PL/SQL Blocks
Purpose
When you put something into a box you intuitively know that
the box has consistent properties. It has sides, a bottom, and
a top that can be opened and closed.
In PL/SQL, you put your programming instructions into block
structures that also have consistent properties.
PLSQL1-3
Creating PL/SQL Blocks
Purpose
Here you will learn the structure of a PL/SQL block and create
one kind of block: an anonymous block.
After learning about the different environments into which
you can develop your PL/SQL programs, you will also begin
coding PL/SQL in the Application Express development
environment.
PLSQL1-3
Creating PL/SQL Blocks
Description
Declarative
(optional)
Executable
(mandatory)
Exception
handling
(optional)
PLSQL1-3
Creating PL/SQL Blocks
Description
Inclusion
Declarative
(DECLARE)
Optional
Executable
(BEGIN
END;)
Mandatory
Optional
Exception
(EXCEPTION)
PLSQL1-3
Creating PL/SQL Blocks
PLSQL1-3
Creating PL/SQL Blocks
PLSQL1-3
Creating PL/SQL Blocks
Anonymous Blocks
Characteristics of anonymous blocks:
Unnamed block
Not stored in the database
Declared inline at the point in an application where it is
executed
Compiled each time the application is executed
Passed to the PL/SQL engine for execution at run time
Cannot be invoked or called because it does not have a name
and does not exist after it is executed
PLSQL1-3
Creating PL/SQL Blocks
10
Anonymous Blocks
[DECLARE]
BEGIN
--statements
[EXCEPTION]
END;
PLSQL1-3
Creating PL/SQL Blocks
11
PLSQL1-3
Creating PL/SQL Blocks
12
PLSQL1-3
Creating PL/SQL Blocks
13
Subprograms
Subprograms:
PROCEDURE name
IS
--variable declaration(s)
BEGIN
--statements
[EXCEPTION]
END;
FUNCTION name
RETURN datatype
--variable declaration(s)
IS
BEGIN
--statements
RETURN value;
[EXCEPTION]
END;
PLSQL1-3
Creating PL/SQL Blocks
14
Subprograms
Subprograms:
Can be declared as procedures or as functions
Procedure: Performs an action
Function: Computes and returns a value
PLSQL1-3
Creating PL/SQL Blocks
15
Examples of Subprograms
Procedure to print the current date
CREATE PROCEDURE print_date IS
v_date VARCHAR2(30);
BEGIN
SELECT TO_CHAR(SYSDATE,'Mon DD, YYYY')
INTO v_date
FROM DUAL;
DBMS_OUTPUT.PUT_LINE(v_date);
END;
PLSQL1-3
Creating PL/SQL Blocks
16
Program Constructs
The following table outlines a variety of different PL/SQL
program constructs that use the basic PL/SQL block. The
constructs are available based on the environment in which
they are executed.
Tools constructs
Anonymous blocks
Stored packages
Application packages
Database triggers
Application triggers
Object types
Object types
PLSQL1-3
Creating PL/SQL Blocks
17
A component of Application
Express.
SQL*Plus
A command-line application.
SQL Developer
JDeveloper
A Windows-based application.
Application Express
A web-browser application.
PLSQL1-3
Creating PL/SQL Blocks
18
PLSQL1-3
Creating PL/SQL Blocks
19
PLSQL1-3
Creating PL/SQL Blocks
20
SQL Commands
You can use SQL
Commands to enter and
run a single SQL
statement or a single
PL/SQL block.
A SQL script can contain
one or more SQL
statements and/or
PL/SQL blocks. Use SQL
Scripts to enter and run
multi-statement scripts.
PLSQL1-3
Creating PL/SQL Blocks
21
PLSQL1-3
Creating PL/SQL Blocks
22
Using DBMS_OUTPUT.PUT_LINE
Lets add a call to DBMS_OUTPUT.PUT_LINE. Now you can see
the result!
PLSQL1-3
Creating PL/SQL Blocks
23
Using DBMS_OUTPUT.PUT_LINE
The DBMS_OUTPUT.PUT_LINE allows you to display results so
that you can check that your block is working correctly. It
allows you to display one character string at a time, although
this can be concatenated.
DECLARE
v_emp_count
NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('PL/SQL is easy so far!');
SELECT COUNT(*) INTO v_emp_count FROM employees;
DBMS_OUTPUT.PUT_LINE('There are '||v_emp_count||'
rows in the employees table');
END;
PLSQL1-3
Creating PL/SQL Blocks
24
Terminology
Key terms used in this lesson included:
Anonymous PL/SQL block
Compiler
Subprograms
Procedures
Functions
PLSQL1-3
Creating PL/SQL Blocks
25
Summary
In this lesson, you should have learned how to:
Describe the structure of a PL/SQL block
Identify the different types of PL/SQL blocks
Identify PL/SQL programming environments
Create and execute an anonymous PL/SQL block
Output messages in PL/SQL
PLSQL1-3
Creating PL/SQL Blocks
26