Procedures Oracle & MySQL

Slides:



Advertisements
Similar presentations
Chapter 9: Advanced SQL and PL/SQL Topics Guide to Oracle 10g.
Advertisements

3 Copyright © 2004, Oracle. All rights reserved. Creating Packages.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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:
Stored Procedures Functions Packages
Copyright  Oracle Corporation, All rights reserved. 3 Creating Procedures.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Advanced SQL: Cursors & Stored Procedures
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
What is a Package? A package is an Oracle object, which holds other objects within it. Objects commonly held within a package are procedures, functions,
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 8 Program Unit Dependencies.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 10 Oracle-Supplied Packages, Dynamic SQL, and Hiding Source Code.
A procedure is a module performing one or more actions; it does not need to return any values. The syntax for creating a procedure is as follows: CREATE.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
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.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
What Are Subprograms? Subprograms are named PL/SQL blocks that can take parameters and be invoked. Subprograms allow decomposition of a program into logical.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
INLS 623– Stored Procedures
Creating Stored Functions
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Stored Procedures.
Chapter 8 Dependencies, Privileges and Compilation Oracle11g:
Creating Stored Procedures and Functions
Chapter 10 Oracle11g: PL/SQL Programming Oracle-Supplied Packages,
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
UNIT - V STORED PROCEDURE.
Introduction to PL/SQL
PL/SQL Scripting in Oracle:
Oracle Stored Procedures and Functions
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Procedures Organized by Farrokh Alemi, Ph.D. Narrated by Yara Alemi
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 8 Advanced SQL.
MATERI PL/SQL Procedures Functions Packages Database Triggers
Oracle Stored Procedures and Functions
Prof. Arfaoui. COM390 Chapter 9
Prof. Arfaoui. COM390 Chapter 6
Handling Data in PL/SQL Blocks
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

Procedures Oracle & MySQL Prof. Arfaoui. COM390 Chapter 5 Oracle11g: PL/SQL Programming

Chapter Objectives After completing this lesson, you should be able to understand: Named program units Creating a procedure Calling a procedure from another procedure Using the DESCRIBE command with procedures Debugging procedures using DBMS_OUTPUT Using subprograms The scope of variables, exception handling and transaction control Removing procedures Oracle11g: PL/SQL Programming

Brewbean’s Challenge Develop programming modules for specific tasks such as calculating taxes or updating inventory Oracle11g: PL/SQL Programming

Named Program Units PL/SQL blocks executed thus far have been anonymous blocks Now we will assign a name to the block and save it in the database as a stored program unit This makes program units reusable Oracle11g: PL/SQL Programming

Stored Routines Procedures, Functions, Triggers, Packages Fast - A stored routine is held on the database server. Reusability - A stored routine is created once but used many times. Code efficiency - Stored routines also reduce code duplication. Debugging and testing an application also becomes easier. Application security - Application only sees the data it needs. Oracle11g: PL/SQL Programming

Create Procedure Statement Syntax Oracle11g: PL/SQL Programming

Parameters – Make Program Units Reusable Mechanisms used to send values in and out of program units IN – is the default mode. When you define an IN parameter in a stored procedure, the calling program has to pass an argument to the stored procedure. In addition, the value of an IN parameter is protected. OUT – the value of an OUT parameter can be changed inside the stored procedure and its new value is passed back to the calling program. IN OUT – an IN OUT parameter is a combination of IN and OUT parameters. The calling program may pass the argument. The stored procedure can modify the IN OUT parameter. Pass the new value back to the calling program. Oracle11g: PL/SQL Programming

Create Procedure - Oracle Procedure to determine shipping cost Oracle DBMS CREATE OR REPLACE PROCEDURE SHIP_COST_SP( p_qty IN Numeric, p_ship OUT Numeric) AS BEGIN IF p_qty > 10 THEN p_ship := 11.00; ELSIF p_qty > 5 THEN p_ship := 8.00; ELSE p_ship := 5.00; END IF; END; Oracle11g: PL/SQL Programming

Create Procedure - MySQL MySQL DBMS DELIMITER // CREATE PROCEDURE SHIP_COST_SP(IN p_qty Numeric, OUT p_ship Numeric) BEGIN IF p_qty > 10 THEN SET p_ship = 11.00; ELSEIF p_qty > 5 THEN SET p_ship = 8.00; ELSE SET p_ship = 5.00; END IF; END// DELIMITER;

Execute Procedure - Oracle Oracle DBMS DECLARE lv_ship_num NUMERIC(6,2); BEGIN SHIP_COST_SP(7 , lv_ship_num); DBMS_OUTPUT.PUT_LINE('Ship Cost = ' || lv_ship_num); END; Declare a variable to hold value from OUT parameter Call procedure addressing both parameters Display value returned to verify Note: Parameter arguments are passed positionally by default Oracle11g: PL/SQL Programming

Execute Procedure - MySQL MySQL DBMS CALL SHIP_COST_SP(7 , @lv_ship_num); SELECT CONCAT('Ship Cost = ' , @lv_ship_num ) AS "OUTPUT";

IN OUT mode - Oracle Oracle DBMS Send value in and out via the same parameter Oracle DBMS CREATE OR REPLACE PROCEDURE phone_fmt_sp(p_phone IN OUT VARCHAR2) AS BEGIN p_phone := '(' || SUBSTR(p_phone,1,3) || ')' || SUBSTR(p_phone,4,3) || '-' || SUBSTR(p_phone,7,4); END; Oracle11g: PL/SQL Programming

IN OUT mode - MySQL MySQL DBMS DELIMITER // CREATE PROCEDURE phone_fmt_sp (INOUT p_phone VARCHAR(25)) BEGIN SET p_phone = CONCAT('(' , SUBSTR(p_phone,1,3) , ')' , SUBSTR(p_phone,4,3) , '-' , SUBSTR(p_phone,7,4)); END//

Execute Procedure - Oracle Oracle DBMS Declare fmt_Phone varchar(200) :='7181234567'; begin phone_fmt_sp(fmt_Phone); DBMS_OUTPUT.PUT_LINE(fmt_Phone); end;

Execute Procedure - MySQL MySQL DBMS set @fmt_Phone = '7181234567'; CALL phone_fmt_sp(@fmt_Phone); select @fmt_Phone AS "OUTPUT";

Calling a Procedure from another procedure Oracle DBMS CREATE OR REPLACE PROCEDURE ORDER_TOTAL_SP (p_bsktid IN number, p_cnt out number, p_sub out number, p_ship out number, p_total out number) AS BEGIN DBMS_OUTPUT.PUT_LINE('order total proc called'); SELECT SUM(quantity), SUM(quantity * price) into p_cnt, p_sub FROM bb_basketitem WHERE idbasket = p_bsktid; ship_cost_sp(p_cnt, p_ship); p_total := NVL(p_sub,0) + NVL(p_ship,0); DBMS_OUTPUT.PUT_LINE('order total proc ended'); END; Calling procedure ship_cost_sp Oracle11g: PL/SQL Programming

Remove a Procedure DROP PROCEDURE procedure_name; Oracle11g: PL/SQL Programming

TO DO Execute ORDER_TOTAL_SP in Oracle Create the equivalent MySQL Stored procedure to ORDER_TOTAL_SP Call ORDER_TOTAL_SP in MySQL

DESCRIBE Command | Oracle & MySQL Lists the parameters of a program unit Oracle: DESCRIBE ORDER_TOTAL_SP; MySQL: SHOW CREATE PROCEDURE phone_fmt_sp; Oracle11g: PL/SQL Programming

Debugging with DBMS_OUTPUT Oracle11g: PL/SQL Programming

Debugging with DBMS_OUTPUT Oracle11g: PL/SQL Programming

Subprograms A program unit defined within another program unit: Must be declared in the DECLARE section of the containing program unit Can only be referenced by the containing program unit Oracle11g: PL/SQL Programming

Variable Scope When nesting blocks, are variables shared? Inner blocks can use variables from outer blocks Oracle11g: PL/SQL Programming

Variable Scope (continued) Oracle11g: PL/SQL Programming

Exception-HandlingFlow Oracle11g: PL/SQL Programming

Transaction Control Scope The scope refers to the group of DML statements that are affected by a particular transaction control statement By default, a session has a single DML queue and a transaction control statement would affect all DML in the queue regardless of which program unit initiated the statement DML statements of a program unit can be treated separately or as an autonomous transaction Oracle11g: PL/SQL Programming

Autonomous Transaction The pragma instructs the PL/SQL compiler to establish a PL/SQL block as autonomous or independent. Oracle11g: PL/SQL Programming

Summary Named program unit assigns a name to a program unit so it can be reused Parameters are used to pass values in and out of program units Stored program units are saved in the database Parameter modes include: IN, OUT, and IN OUT Use DBMS_OUTPUT.PUT_LINE statement to debug Oracle11g: PL/SQL Programming

Summary (continued) A subprogam is a procedure declared within another procedure Variable scope must be considered with nested blocks Autonomous transactions must be explicitly created Remove a procedure with the DROP PROCEDURE command Oracle11g: PL/SQL Programming