PL/SQL Continued. So far… Anonymous blocks Procedures Have you tried… –Downloading and running the dispcust.sql sample from your web page? –Writing your.

Slides:



Advertisements
Similar presentations
Oracle PL/SQL IV Exceptions Packages.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
PL_SQL Explanation of DISPCUST.SQL. 1 of 3 DECLARE cnum builder.customer.customer_id%type; cname builder.customer.customer_name%type; caddr builder.customer.customer_address%type;
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Building a database. Implementation of desirable features Integrity –A field’s validation can be declared when the field is declared. If this validation.
Some relationship types Using the Builder2 schema.
1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements.
Specification for Write Docket From Joe’s Yard. Requirements We’ve seen the docket and know it needs to be filled. To fill the docket, we need to: –Generate.
Creating Packages. 2 home back first prev next last What Will I Learn? Describe the reasons for using a package Describe the two components of a package:
Normalisation up to 1NF Bottom-up Approach to Data Modelling.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Programming in Oracle with PL/SQL
PL/SQL The Oracle Programming Language. Purpose SQL is a non-procedural language. Often we need to put structure on transactions, so we use the supplemental.
Exception types In Oracle PL/SQL. Types of exceptions Named system exceptions –Raised as a result of an error in PL/SQL or RDBMS processing. Named programmer-defined.
Week 2 Lecture 2 Structure of a database. External Schema Conceptual Schema Internal Schema Physical Schema.
Project Implementation for COSC 5050 Distributed Database Applications Lab5.
4 Copyright © 2004, Oracle. All rights reserved. Database Interfaces.
Project Implementation for COSC 5050 Distributed Database Applications Lab2.
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Oracle’s take on joins Where it differs from ANSI standard.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
BA372 Stored Procedures and Triggers Lab. What needs to be done to change a customer’s credit limit? Who am I? May I? Do it Log it Display A database.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
CSIT 313 DB PROGRAMMING EXCEPTION HANDLING. In PL/SQL, an error condition is called an exception. An exception can be either –internally defined (by the.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Database Technology Jing Shen.
INFO1408 Database Design Concepts Week 16: Introduction to Database Management Systems Continued.
Advanced SQL: Triggers & Assertions
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
Internal and Confidential Cognos CoE COGNOS 8 – Event Studio.
 Shopping Basket  Stages to maintain shopping basket in framework  Viewing Shopping Basket.
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
Lab 9 Moving on to implementation and testing the package.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
A Guide to SQL, Sixth Edition 1 Chapter 5 Updating Data.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must.
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.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Oracle SQL.
Task oriented processing
Creating Stored Procedures and Functions
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
Introduction to Triggers
Handling Exceptions.
Advanced PL/SQL Programing
Handling Exceptions.
Handling Exceptions.
Database Management Systems 2
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Prof. Arfaoui. COM390 Chapter 9
Presentation transcript:

PL/SQL Continued

So far… Anonymous blocks Procedures Have you tried… –Downloading and running the dispcust.sql sample from your web page? –Writing your own anonymous block with embedded procedure?

Today… Store procedures and functions for independent use against the schema. Combine procedures and functions to give packages. Look at how errors can be: –Handled –Trapped and passed back to the calling environment.

Storing procedures Look ahead at the next slide. –It shows the procedure that we used in the sample anonymous block. –It is a local procedure, that ceases to exist when the program stops running. –Often we need to reuse functionality. –The procedure can be defined as part of the schema. See the slide after next.

The procedure in dispcust procedure get_cust_details ( cust_noinbuilder.customer.customer_id%type, cust_nameoutbuilder.customer.customer_name%type, cust_addroutbuilder.customer.customer_address%type, statusoutboolean) is begin status := true; select builder.customer.customer_name, builder.customer.customer_address into cust_name, cust_addr from builder.customer where builder.customer.customer_id = cust_no; exception when no_data_found then dbms_output.put_line('ERROR'); status := false; end;

… and independently… Create or replace procedure get_cust_details ( cust_noinbuilder.customer.customer_id%type, cust_nameoutbuilder.customer.customer_name%type, cust_addroutbuilder.customer.customer_address%type, statusoutboolean) as begin status := true; select builder.customer.customer_name, builder.customer.customer_address into cust_name, cust_addr from builder.customer where builder.customer.customer_id = cust_no; exception when no_data_found then dbms_output.put_line('ERROR'); status := false; end;

Functions for independent use create or replace Function total_emp_sales( emp_no in staff.staff_no%type) Return number as sales number; Begin select sum(unit_price * quantityrequired) into sales from staff join corder on staff.staff_no = corder.staffpaid join corderline on corder.corderno = corderline.corderno join stock on corderline.stock_code = stock.stock_code where staff.staff_no = emp_no; return (sales); End;

To call this function: sql>select staff_no, total_emp_sales(staff_no) from staff;

Packages Often it makes sense to keep functions and procedures that relate to the same functionality or group of tables together. This grouping can be known as a package. –It is somewhat similar to a package of classes. An example would be the set of steps required to process orders.

Processing orders… Set up a customer order –Does the customer exist in the system already? Should we add the customer? –What will the order number be? Add a detail line to the customer order –Have we enough stock? What’ll we do if we don’t? –Did the stock fall below the reorder level? What’ll we do if it did? Issue the stock for the order –What if there’s an error?

Before we begin: Decide on: –A logging strategy –An error handling strategy for Expected errors Unexpected errors –Commit and rollback strategy –Maintenance of sequences strategy

Answers… Set up a customer order –Does the customer exist in the system already? Should we add the customer? –What will the order number be? Answers: –Provide a facility for the user to look up the customer by name and address. –Provide a facility to add a customer. –Use a SEQUENCE to create a unique order number.

…Answers… Add a detail line to the customer order –Have we enough stock? What’ll we do if we don’t? –Did the stock fall below the reorder level? What’ll we do if it did? Answers –Cancel the detail line, log the problem and return an error if we don’t have enough stock –Log the fact that the reorder quantity has been reached.

Logging errors Some of the logging will be used when the supplier orders are being set up, so we can formalise this procedure: Set up logging

The ‘add order’ procedure Take in the customer number, the employee number and the order date. Take the next sequential order number. Add a new order, leaving the ‘staff issued’ field as null. Inform the operator of any errors and also log them in a corder_error table, which has 3 columns: –Transaction date –Order number –Message.

Add corderline procedure Take in the order number, the stock code and the quantityrequired. If stock_level – quantityrequired > reorder_level then –Update stock level to stock_level - quantityrequired –Add new corderline with corderno, stockcode, quantityrequired Elseif stock_level – quantityrequired quantityrequired –Log the reorder requirement –Update stock_level to stock_level - quantityrequired –Add new corderline with corderno, stockcode, quantityrequired else –Log the error –Control Failure out of the procedure End Introduce a corderline_error table which has 4 columns: –Transaction date –Corderno –Stock code –Message

Sequences Sequences: –Fields that are set up to autoincrement every time they are used: create sequence custseq increment by 1 start with 100 maxvalue cycle; –To increment the sequence: Select custseq.nextval from dual; Or Customer_id = custseq.nextval;

Commit and Rollback Commit confirms work done on the database Rollback undoes work done since last commit, exit (PL/SQL), table drop, create or alter. It is wise to commit work at logical breakpoints – at the end of a sequence of transactions. –In this case, the commits should happen when all of the order lines had been put in against an order and the customer had paid. When the order was updated as issued.

Combine procedures and functions to give packages. See accompanying handout and sidebar link.

To create the package… Package created. No errors. Package body created. No errors.

To run a procedure from the package: SQL> exec custorders.add_corderline(108,'B111',20) PL/SQL procedure successfully completed. Note: there were not enough ‘B111’s to fill this orderline – see next slide.

SQL> select * from corderline; QUANTITYREQUIRED CORDERNO STOCK A A B C C D D E E D B111 QUANTITYREQUIRED CORDERNO STOCK E rows selected.

What about the error table? SQL> select * from corderline_error; ERRORDATE ONUM SCODE ERRMSG NOV B111 Do not have enough to sell SQL>