Introduction to PL/SQL
Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Objectives
After completing this lesson, you should be able to do the
following:
• Explain the need for PL/SQL
• Explain the benefits of PL/SQL
• Identify the different types of PL/SQL blocks
• Output messages in PL/SQL
1-2 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Agenda
• Understanding the benefits and structure of PL/SQL
• Examining PL/SQL blocks
• Generating output messages in PL/SQL
1-3 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
About PL/SQL
PL/SQL:
• Stands for “Procedural Language extension to SQL”
• Is Oracle Corporation’s standard data access language for
relational databases
• Seamlessly integrates procedural constructs with SQL
1-4 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
About PL/SQL
PL/SQL:
• Provides a block structure for executable units of code.
Maintenance of code is made easier with such a well-
defined structure.
• Provides procedural constructs such as:
– Variables, constants, and data types
– Control structures such as conditional statements and loops
– Reusable program units that are written once and executed
many times
1-5 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
PL/SQL Run-Time Architecture
PL/SQL block
procedural
Procedural statement
executor
PL/SQL
PL/SQL Engine
SQL
Oracle Server
SQL statement executor
1-6 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Benefits of PL/SQL
• Integration of procedural constructs with SQL
• Improved performance
SQL 1
SQL 2
…
SQL
IF...THEN
SQL
ELSE
SQL
END IF;
SQL
1-7 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Benefits of PL/SQL
• Modularized program development
• Integration with Oracle tools
• Portability
• Exception handling
1-8 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
PL/SQL Block Structure
• DECLARE (optional)
– Variables, cursors, user-defined exceptions
• BEGIN (mandatory)
– SQL statements
– PL/SQL statements
• EXCEPTION (optional)
– Actions to perform
when exceptions occur
• END; (mandatory)
1 - 10 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Agenda
• Understanding the benefits and structure of PL/SQL
• Examining PL/SQL blocks
• Generating output messages in PL/SQL
1 - 12 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Block Types
Procedure Function Anonymous
PROCEDURE name FUNCTION name [DECLARE]
IS RETURN datatype
IS
BEGIN BEGIN BEGIN
--statements --statements --statements
RETURN value;
[EXCEPTION] [EXCEPTION] [EXCEPTION]
END; END; END;
1 - 13 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Program Constructs
Database Server
Tools Constructs Constructs
Anonymous blocks Anonymous blocks
Application procedures Stored procedures or
or functions functions
Application packages Stored packages
Application triggers Database triggers
Object types Object types
1 - 15 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Examining an Anonymous Block
An anonymous block in the SQL Developer workspace:
1 - 17 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Executing an Anonymous Block
Click the Run Script button to execute the anonymous block:
Run Script (or F5)
1 - 18 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Agenda
• Understanding the benefits and structure of PL/SQL
• Examining PL/SQL blocks
• Generating output messages in PL/SQL
1 - 19 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Enabling Output of a PL/SQL Block
1. To enable output in SQL Developer, execute the following
command before running the PL/SQL block:
SET SERVEROUTPUT ON
2. Use a predefined Oracle package and its procedure in the
anonymous block:
– DBMS_OUTPUT.PUT_LINE
DBMS_OUTPUT.PUT_LINE(' The First Name of the
Employee is ' || v_fname);
…
1 - 20 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Viewing the Output of a PL/SQL Block
Press F5 to execute the
command and PL/SQL
block.
1 - 21 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Quiz
A PL/SQL block must consist of the following three sections:
• A Declarative section, which begins with the keyword
DECLARE and ends when the executable section starts.
• An Executable section, which begins with the keyword
BEGIN and ends with END.
• An Exception handling section, which begins with the
keyword EXCEPTION and is nested within the executable
section.
a. True
b. False
1 - 22 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Summary
In this lesson, you should have learned how to:
• Integrate SQL statements with PL/SQL program constructs
• Describe the benefits of PL/SQL
• Differentiate between PL/SQL block types
• Output messages in PL/SQL
1 - 23 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Practice 1: Overview
This practice covers the following topics:
• Identifying the PL/SQL blocks that execute successfully
• Creating and executing a simple PL/SQL block
1 - 24 Copyright © 2010, Oracle and/or its affiliates. All rights reserved.