26
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL [email protected]

Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

  • Upload
    others

  • View
    70

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1Copyright © 2004, Oracle. All rights reserved.

Introduction to PL/SQL

[email protected]

Page 2: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-2 Copyright © 2004, Oracle. 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• Use iSQL*Plus as a development environment for

PL/SQL • Output messages in PL/SQL

Page 3: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-3 Copyright © 2004, Oracle. All rights reserved.

What Is 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

Page 4: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-4 Copyright © 2004, Oracle. 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 types– Control structures such as conditional statements

and loops– Reusable program units that are written once and

executed many times

Page 5: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-5 Copyright © 2004, Oracle. All rights reserved.

PL/SQL Environment

PL/SQL Engine

Oracle Database Server

SQL Statement Executor

Procedural Statement Executor

procedural

SQL

PL/SQLBlock

Page 6: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-6 Copyright © 2004, Oracle. All rights reserved.

Benefits of PL/SQL

SQLIF...THEN

SQLELSE

SQLEND IF;SQL

• Integration of procedural constructs with SQL• Improved performance

SQL 1

SQL 2…

Page 7: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-7 Copyright © 2004, Oracle. All rights reserved.

Benefits of PL/SQL

• Modularized program development• Integration with Oracle tools• Portability• Exception handling

Page 8: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-8 Copyright © 2004, Oracle. All rights reserved.

Benefits of PL/SQL

Page 9: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-9 Copyright © 2004, Oracle. 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 performwhen errors occur

END; (Mandatory)

Page 10: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-10 Copyright © 2004, Oracle. All rights reserved.

PL/SQL Block Structure

Page 11: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-11 Copyright © 2004, Oracle. All rights reserved.

Block Types

Anonymous ProcedureFunction

[DECLARE]

BEGIN --statements

[EXCEPTION]

END;

PROCEDURE nameIS

BEGIN --statements

[EXCEPTION]

END;

FUNCTION nameRETURN datatypeISBEGIN --statements RETURN value;[EXCEPTION]

END;

Page 12: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-12 Copyright © 2004, Oracle. All rights reserved.

Block Types

Page 13: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-13 Copyright © 2004, Oracle. All rights reserved.

Program Constructs

Application triggers

Application packages

Application proceduresor functions

Anonymous blocks

Tools Constructs

Object types

Database triggers

Stored packages

Stored procedures orfunctions

Anonymous blocks

Database ServerConstructs

Object types

Page 14: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-14 Copyright © 2004, Oracle. All rights reserved.

Program Constructs

Page 15: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-15 Copyright © 2004, Oracle. All rights reserved.

PL/SQL Programming Environments

Page 16: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-16 Copyright © 2004, Oracle. All rights reserved.

PL/SQL Programming Environments

iSQL*Plus

Page 17: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-17 Copyright © 2004, Oracle. All rights reserved.

PL/SQL Programming Environments

Page 18: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-18 Copyright © 2004, Oracle. All rights reserved.

iSQL*Plus Architecture

Page 19: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-19 Copyright © 2004, Oracle. All rights reserved.

Create an Anonymous Block

Type the anonymous block in the iSQL*Plus workspace:

Page 20: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-20 Copyright © 2004, Oracle. All rights reserved.

Execute an Anonymous Block

Click the Execute button to execute the anonymous block:

PL\SQL procedure successfully completed.

Page 21: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-21 Copyright © 2004, Oracle. All rights reserved.

Test the Output of a PL/SQL Block

• Enable output in iSQL*Plus with the commandSET SERVEROUTPUT ON

• Use a predefined Oracle package and its procedure:– DBMS_OUTPUT.PUT_LINE

SET SERVEROUTPUT ON…DBMS_OUTPUT.PUT_LINE(' The First Name of the Employee is ' || f_name);…

Page 22: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-22 Copyright © 2004, Oracle. All rights reserved.

Test the Output of a PL/SQL Block

Page 23: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-23 Copyright © 2004, Oracle. All rights reserved.

Summary

In this lesson, you should have learned how to:• Integrate SQL statements with PL/SQL program

constructs• Identify the benefits of PL/SQL• Differentiate different PL/SQL block types• Use iSQL*Plus as the programming environment

for PL/SQL• Output messages in PL/SQL

Page 24: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-24 Copyright © 2004, Oracle. All rights reserved.

Practice 1: Overview

This practice covers the following topics:• Identifying which PL/SQL blocks execute

successfully• Creating and executing a simple PL/SQL block

Page 25: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-25 Copyright © 2004, Oracle. All rights reserved.

Practice 1

Page 26: Introduction to PL/SQL · • Integrate SQL statements with PL/SQL program constructs • Identify the benefits of PL/SQL • Differentiate different PL/SQL block types • Use iSQL*Plus

1-26 Copyright © 2004, Oracle. All rights reserved.