Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.

Slides:



Advertisements
Similar presentations
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives u How to use the SQL programming language u How to use SQL cursors u How to create.
Advertisements

PL/SQL.
The Web Warrior Guide to Web Design Technologies
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Oracle PL/SQL Eyad Husni Elshami. Why PL/SQL Block Structures: – PL/SQL consists of blocks of code, which can be nested within each other. Each block.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Introduction to PL/SQL
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
Oracle PL/SQL. PL/SQL Originally modeled after ADA Originally modeled after ADA Created for Dept. of DefenseCreated for Dept. of Defense Allows expanded.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
Database Application Development using PL/SQL Programming.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
© 2007 by Prentice Hall7-1 Introduction to Oracle 10g Chapter 7 Using PL/SQL to Your Advantage James Perry and Gerald Post.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Variables and control statements in PL\SQL Chapter 10.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
implicit and an explicit cursor
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
A Guide to SQL, Seventh Edition
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Interacting with the Oracle Server
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Handling Exceptions.
PL/SQL Scripting in Oracle:
Handling Exceptions.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
variables and control statements in PL\SQL
Chapter 8 Advanced SQL.
PL/SQL Declaring Variables.
MATERI PL/SQL Procedures Functions Packages Database Triggers
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

Chapter 15 Introduction to PL/SQL

Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections of a PL/SQL block and describe their contents  Identify the mandatory and optional sections of a PL/SQL block  Identify an anonymous block and its use  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections of a PL/SQL block and describe their contents  Identify the mandatory and optional sections of a PL/SQL block  Identify an anonymous block and its use

Chapter Objectives  Describe how to execute a PL/SQL block  Explain the purpose of a variable  Explain the difference between a constant and a variable  Identify valid variable names  List the valid datatypes for PL/SQL variables  Describe how to execute a PL/SQL block  Explain the purpose of a variable  Explain the difference between a constant and a variable  Identify valid variable names  List the valid datatypes for PL/SQL variables

Chapter Objectives  Assign a dynamic datatype for a PL/SQL variable  Initialize a PL/SQL variable  Use DML statements in a PL/SQL block  Determine when it is appropriate to use an IF statement  Identify all the clauses of an IF statement, and state when they should be used  Assign a dynamic datatype for a PL/SQL variable  Initialize a PL/SQL variable  Use DML statements in a PL/SQL block  Determine when it is appropriate to use an IF statement  Identify all the clauses of an IF statement, and state when they should be used

Chapter Objectives  Create an IF statement  Identify the purpose of a loop, and name the types of loops available in Oracle9i  Create a basic loop  Create a FOR loop  Create a WHILE loop  Create an IF statement  Identify the purpose of a loop, and name the types of loops available in Oracle9i  Create a basic loop  Create a FOR loop  Create a WHILE loop

PL/SQL  Procedure Language SQL  Advanced 4 th generation programming language  Procedure Language SQL  Advanced 4 th generation programming language

Advantages of PL/SQL  Can include error handling and control structures  Can be stored and used by various application programs or users  Allows for tighter security by granting privileges for executing stored procedures rather than directly on database object  Can include error handling and control structures  Can be stored and used by various application programs or users  Allows for tighter security by granting privileges for executing stored procedures rather than directly on database object

Types of Blocks  Function  Procedure  Anonymous block  Function  Procedure  Anonymous block

Function  Named block that is stored on the Oracle9i server  Accepts zero or more input parameters  Returns one value  Named block that is stored on the Oracle9i server  Accepts zero or more input parameters  Returns one value

Procedure  Named block  Can process several variables  Returns no values  Interacts with application program using IN, OUT, or INOUT parameters  Named block  Can process several variables  Returns no values  Interacts with application program using IN, OUT, or INOUT parameters

Anonymous Block  Not stored since it cannot be referenced by a name  Usually embedded in an application program, stored in a script file, or manually entered when needed  Not stored since it cannot be referenced by a name  Usually embedded in an application program, stored in a script file, or manually entered when needed

Basic Structure of a Block  Has three sections: –Declarative –Executable –Exception-handling  Executable section is the only required section; the rest are optional  Has three sections: –Declarative –Executable –Exception-handling  Executable section is the only required section; the rest are optional

Declarative Section  Identified by the DECLARE keyword  Used to define variables and constants referenced in the block  Forward execution – variable and constants must be declared before they can be referenced  Identified by the DECLARE keyword  Used to define variables and constants referenced in the block  Forward execution – variable and constants must be declared before they can be referenced

Executable Section  Identified by the BEGIN keyword  Mandatory  Can consist of several SQL and/or PL/SQL statements  Identified by the BEGIN keyword  Mandatory  Can consist of several SQL and/or PL/SQL statements

Exception-handling Section  Identified by the EXCEPTION keyword  Used to display messages or identify other actions to be taken when an error occurs  Addresses errors that occur during a statement’s execution  Identified by the EXCEPTION keyword  Used to display messages or identify other actions to be taken when an error occurs  Addresses errors that occur during a statement’s execution

END Keyword  Used to close a PL/SQL block  Always followed by a semicolon  Used to close a PL/SQL block  Always followed by a semicolon

Example PL/SQL Block

Declaring a Variable  Reserves a temporary storage area in the computer’s memory  Every variable must have: –A name –A datatype  Variables can be initialized  Reserves a temporary storage area in the computer’s memory  Every variable must have: –A name –A datatype  Variables can be initialized

Variable Names  Variable name can consist of up to 30 characters, numbers, or special symbols  Variable name must begin with a character  Variable name can consist of up to 30 characters, numbers, or special symbols  Variable name must begin with a character

Constants  Variables that have a value that does not change during the execution of the block  Optional CONSTANT keyword can be used to designate a constant in the block’s declarative section  Variables that have a value that does not change during the execution of the block  Optional CONSTANT keyword can be used to designate a constant in the block’s declarative section

PL/SQL Datatypes  Scalar – holds a single value  Composite – collection of grouped data treated as one unit  Reference – holds pointers to other program items  Large Object (LOB) – holds location of large objects  Scalar – holds a single value  Composite – collection of grouped data treated as one unit  Reference – holds pointers to other program items  Large Object (LOB) – holds location of large objects

PL/SQL Scalar Datatypes

Variable Initialization  Use DEFAULT keyword or (:=) assignment operator  Variable must be initialized if it is assigned a NOT NULL constraint  Use DEFAULT keyword or (:=) assignment operator  Variable must be initialized if it is assigned a NOT NULL constraint

Variable Initialization Examples

SELECT Statement Requires use of INTO clause to identify variable assigned to each data element

SELECT Statement Example

Cursors  Implicit cursor – created for DML operations or a SELECT statement that retrieves only one row of results  Explicit cursor – required for SELECT statements retrieving more than one row of results  Implicit cursor – created for DML operations or a SELECT statement that retrieves only one row of results  Explicit cursor – required for SELECT statements retrieving more than one row of results

Execution Control  IF statement – executes statements based on a condition  Basic loop – executes statements until condition in EXIT clause is TRUE  FOR loop – uses counter  WHILE loop – executes statements until condition is FALSE  IF statement – executes statements based on a condition  Basic loop – executes statements until condition in EXIT clause is TRUE  FOR loop – uses counter  WHILE loop – executes statements until condition is FALSE

IF Statement Syntax

IF Statement Example

Basic Loop Syntax

Basic Loop Example

FOR Loop Syntax

FOR Loop Example

WHILE Loop Syntax

WHILE Loop Example

Nested Loops  Any type of loop can be nested inside another loop  Execution of the inner loop must be completed before control is returned to the outer loop  Any type of loop can be nested inside another loop  Execution of the inner loop must be completed before control is returned to the outer loop

Nested Loop Example