Objectives
After completing this lesson, you should be able to do the
following:
1
• Explain the need for PL/SQL
Introduction to PL/SQL • Explain the benefits of PL/SQL
• Identify the different types of PL/SQL blocks
• Output messages in PL/SQL
Copyright © 2009, Oracle. All rights reserved. 1-2 Copyright © 2009, Oracle. All rights reserved.
1 2
About PL/SQL About PL/SQL
PL/SQL: PL/SQL:
• Stands for “Procedural Language extension to SQL” • Provides a block structure for executable units of code.
• Is Oracle Corporation’s standard data access language for Maintenance of code is made easier with such a well-
relational databases defined structure.
• Seamlessly integrates procedural constructs with SQL • 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-3 Copyright © 2009, Oracle. All rights reserved. 1-4 Copyright © 2009, Oracle. All rights reserved.
3 4
PL/SQL Environment Benefits of PL/SQL
• Integration of procedural constructs with SQL
• Improved performance
PL/SQL engine
procedural Procedural
PL/SQL statement
block executor SQL 1
SQL
SQL 2
…
SQL statement SQL
executor IF...THEN
SQL
ELSE
Oracle database server SQL
END IF;
SQL
1-5 Copyright © 2009, Oracle. All rights reserved. 1-6 Copyright © 2009, Oracle. All rights reserved.
5 6
1
Benefits of PL/SQL PL/SQL Block Structure
• Modularized program development • DECLARE (optional)
• Integration with Oracle tools – Variables, cursors, user-defined exceptions
• Portability • BEGIN (mandatory)
• Exception handling – SQL statements
– PL/SQL statements
• EXCEPTION (optional)
– Actions to perform
when errors occur
• END; (mandatory)
1-7 Copyright © 2009, Oracle. All rights reserved. 1-9 Copyright © 2009, Oracle. All rights reserved.
7 9
Block Types Program Constructs
Anonymous Procedure Function
[DECLARE] PROCEDURE name FUNCTION name
IS RETURN datatype
IS Database Server
BEGIN BEGIN BEGIN Tools Constructs Constructs
--statements --statements --statements Anonymous blocks Anonymous blocks
RETURN value;
[EXCEPTION] [EXCEPTION] [EXCEPTION] Application procedures Stored procedures or
or functions functions
END; END; END;
Application packages Stored packages
Application triggers Database triggers
Object types Object types
1 - 11 Copyright © 2009, Oracle. All rights reserved. 1 - 13 Copyright © 2009, Oracle. All rights reserved.
11 13
Create an Anonymous Block Execute an Anonymous Block
Enter the anonymous block in the SQL Developer workspace: Click the Run Script button to execute the anonymous block:
Run Script
1 - 15 Copyright © 2009, Oracle. All rights reserved. 1 - 16 Copyright © 2009, Oracle. All rights reserved.
15 16
2
Test the Output of a PL/SQL Block Test the Output of a PL/SQL Block
• Enable output in SQL Developer by clicking the Enable
DBMS Output button on the DBMS Output tab:
Enable DBMS
Output 1
2
DBMS Output
Tab
• Use a predefined Oracle package and its procedure:
– DBMS_OUTPUT.PUT_LINE
DBMS_OUTPUT.PUT_LINE(' The First Name of the
Employee is ' || v_fname);
…
1 - 17 Copyright © 2009, Oracle. All rights reserved. 1 - 18 Copyright © 2009, Oracle. All rights reserved.
17 18
Quiz Summary
A PL/SQL block must consist of the following three sections: In this lesson, you should have learned how to:
• A Declarative section which begins with the keyword • Integrate SQL statements with PL/SQL program constructs
DECLARE and ends when the executable section starts. • Describe the benefits of PL/SQL
• An Executable section which begins with the keyword • Differentiate between PL/SQL block types
BEGIN and ends with END.
• Output messages in PL/SQL
• An Exception handling section which begins with the
keyword EXCEPTION and is nested within the executable
section.
1. True
2. False
1 - 19 Copyright © 2009, Oracle. All rights reserved. 1 - 20 Copyright © 2009, Oracle. All rights reserved.
19 20
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 - 21 Copyright © 2009, Oracle. All rights reserved.
21