Distributed Database Applications COSC 5050 Week Five.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

Oracle PL/SQL IV Exceptions Packages.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Library example CREATE TABLE books ( isbn VARCHAR2(13) PRIMARY KEY, title VARCHAR2(200), summary VARCHAR2(2000), author VARCHAR2(200), date_published DATE,
Chapter 4B: More Advanced PL/SQL Programming
PL/SQL Agenda: Basic PL/SQL block structure
Programming in Oracle with PL/SQL
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.
Exception Handling in PL/SQL. POINTS TO DISCUSS What is Exception Handling Structure of Exception Handling Section Types of Exceptions.
PL/SQL block has the following structure: DECLARE Declaration statements BEGIN Executable statements EXCEPTION Exception-handling statements END ;
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.
Distributed Database Applications COSC 5050 Week Three.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Bordoloi and Bock EXCEPTIONS. Bordoloi and Bock Errors Two types of errors can be found in a program: compilation errors and runtime errors. There is.
Cursor and Exception Handling By Nidhi Bhatnagar.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
Program with PL/SQL. Interacting with the Oracle Server.
Distributed Database Applications COSC 5050 Week One.
Distributed Database Applications COSC 5050 Week Seven.
L/O/G/O Working with Composite Data Types. Objectives After completing this lesson, you should be able to do the following: –Create user-defined PL/SQL.
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #8 Matthew P. Johnson CISDD, CUNY June, 2004.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
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:
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.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
Database Technology Jing Shen.
Oracle 8i Exception Handling. General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2.
Using SQL in PL/SQL ITEC 224 Database Programming.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Chapter Seventeen Subprogramming Objective: –Procedures –Functions –Packages.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Chapter 18: Exception Handling1 Chapter Eighteen Exception Handling Objective: – Define exceptions – List types of exception handlers – Trap errors – Exception.
Distributed Database Applications COSC 5050 Week Six.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Copyright  Oracle Corporation, All rights reserved. 23 Handling Exceptions.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
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.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
Oracle 数据库应用 -- PL/SQL 进阶 (3) & Oracle DBA 2016/5/ /5/10.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
ITEC 224 Database Programming
CHAPTER 5 EXCEPTION HANDLING
Interacting with the Oracle8 Server
Creating Database Triggers
Interacting with the Oracle Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Handling Exceptions.
SQL Stored Triggers Presented by: Dr. Samir Tartir
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Cursors ITEC 224 Database Programming PL/SQL Lab.
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
SQL PL/SQL Presented by: Dr. Samir Tartir
Database Management Systems 2
Agenda Summary of last class Cursors Loops Records Simple Loops
Handling Exceptions.
Database Management Systems 2
Handling Exceptions.
PRACTICE OVERVIEW PL/SQL Part - 1.
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

Distributed Database Applications COSC 5050 Week Five

Jiangping Wang Webster UniversityDistributed Database Applications Outline Managing PL/SQL Code Exceptions and Error Handling DML and Transaction Management Packages

Jiangping Wang Webster UniversityDistributed Database Applications Data Dictionary USER_ USER_DEPENDENCIES USER_ERRORS USER_OBJECTS USER_SOURCE USER_TRIGGERS USER_ARGUMENTS

Jiangping Wang Webster UniversityDistributed Database Applications Data Dictionary SELECT object_type, object_name, status FROM user_objects WHERE object_type IN ('PACKAGE', 'PACKAGE BODY', 'FUNCTION', 'PROCEDURE', 'TRIGGER' ) ORDER BY object_type, status, object_name; SELECT TEXT FROM USER_SOURCE WHERE NAME = 'ADD_EMPLOYEE'; DESC ADD_EMPLOYEE

Jiangping Wang Webster UniversityDistributed Database Applications Debug PL/SQL Programs Recompile invalid code Source code debugger With SQL Developer Analyzing instead of trying Unit test show err

Jiangping Wang Exception Handler Webster UniversityDistributed Database Applications

Jiangping Wang Webster UniversityDistributed Database Applications Exception Handler System exception An exception that is defined by Oracle and is usually raised by the PL/SQL runtime engine With name or number NO_DATA_FOUND User defined exceptions An exception that is defined by the programmer and is therefore specific to the application

Jiangping Wang Webster UniversityDistributed Database Applications Exception DECLARE l_department department%ROWTYPE; BEGIN SELECT * INTO l_department FROM department WHERE dept_num = 3; DBMS_OUTPUT.PUT_LINE(l_department.dept_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No data!'); WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE('Too many rows'); END; /

Jiangping Wang Webster UniversityDistributed Database Applications Exception create or replace function get_department( ssn_in in employee.ssn%type) return department.dept_name%type is l_dept_name department.dept_name%type; begin select dept_name into l_dept_name from department inner join employee on employee.dept_num = department.dept_num where ssn = ssn_in; -- dbms_output.put_line('dept name: ' || l_dept_name); return l_dept_name; exception when no_data_found then -- no such employee or not in any department! return null; end; /

Jiangping Wang Webster UniversityDistributed Database Applications Exception procedure delete_department (department_id_in in number) is begin delete from department where dept_num = department_id_in; exception when others then if sqlcode = then dbms_output.put_line ('Please delete employees for the department first.'); end if; end; procedure delete_department (department_id_in in number) is begin delete from department where dept_num = department_id_in; end; SQLCODE is a built-in function that returns the number of the last error raised.

Jiangping Wang Declare Exception Declare an exception by listing the name of the exception followed by the keyword EXCEPTION invalid_company_id EXCEPTION; Be referenced in two ways Raise the exception: RAISE invalid_company_id; Handle the raised exception: WHEN invalid_company_id THEN Webster UniversityDistributed Database Applications

Jiangping Wang Webster UniversityDistributed Database Applications Declare Exception procedure calc_annual_sales (company_id_in in company.company_id%type) is invalid_company_id exception; negative_balance exception; duplicate_company boolean; begin … body of executable statements … raise invalid_company_id; exception … when no_data_found –- system exception then … when invalid_company_id then … when negative_balance then … end;

Jiangping Wang Webster UniversityDistributed Database Applications Declare Exception procedure delete_department(department_id_in in number) is l_count number; still_have_employees exception; begin select count(*) into l_count from employee where dept_num = department_id_in; if l_count > 0 then raise still_have_employees; end if; delete from department where dept_num = department_id_in; exception when still_have_employees then dbms_output.put_line ('Please delete employees for the department first.'); end;

Jiangping Wang Webster UniversityDistributed Database Applications Associate Exception Name procedure delete_department(department_id_in in number) is still_have_employees exception; pragma exception_init (still_have_employees, -2292); begin delete from department where dept_num = department_id_in; exception when still_have_employees then dbms_output.put_line ('Please delete employees for the department first.'); end; ORA-2292 violated integrity constraining (OWNER.CONSTRAINT) – child record found. The PRAGMA keyword is used to signify that the remainder of the PL/SQL statement is a pragma, or directive, or pseudoinstruction to the compiler.

Jiangping Wang Associate Exception Name EXCEPTION_INIT Compile-time command or pragma Shot for pragmatic information Instructs the compiler to associate an identifier, declared as an EXCEPTION, with a specific error number Webster UniversityDistributed Database Applications

Jiangping Wang Associate Exception Name Centralize EXCEPTION_INIT into packages Webster UniversityDistributed Database Applications CREATE OR REPLACE PACKAGE dynsql IS invalid_table_name EXCEPTION; PRAGMA EXCEPTION_INIT (invalid_table_name, -903); invalid_column_name EXCEPTION; PRAGMA EXCEPTION_INIT (invalid_column_name, -904);

Jiangping Wang Webster UniversityDistributed Database Applications Raise Exception Oracle might raise the exception when it detects an error Programmer might raise an exception with the RAISE statement RAISE exception_name; RAISE package_name.exception_name; RAISE; -- propagate out

Jiangping Wang Raise Exception Webster UniversityDistributed Database Applications DECLARE invalid_id EXCEPTION; id_value VARCHAR2(30); BEGIN id_value := id_for ('SMITH'); IF SUBSTR (id_value, 1, 1) != 'X' THEN RAISE invalid_id; END IF;... END; IF days_overdue (isbn_in, borrower_in) > 365 THEN RAISE overdue_pkg.book_is_lost; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN -- record all the "context" information errlog.putline (company_id_in); -- pass NO_DATA_FOUND unhandled RAISE;

Jiangping Wang Handling Exception Exception handlers are structured much like a conditional CASE statement Webster UniversityDistributed Database Applications PropertyDescription EXCEPTION WHEN NO_DATA_FOUND THEN executable_statements1; If the NO_DATA_FOUND exception is raised, then execute the first set of statements. WHEN payment_overdue THEN executable_statements2; If the payment is overdue, then execute the second set of statements WHEN OTHERS THEN executable_statements3; END; If any other exception is encountered, then execute the third set of statements.

Jiangping Wang Webster UniversityDistributed Database Applications PROCEDURE add_employee ( ssn in varchar2, fname in varchar2, lname in varchar2, dept_num in number, code in number, sup_ssn in varchar2) IS BEGIN insert into employee values (ssn, fname, lname, dept_num, code, sup_ssn); EXCEPTION WHEN OTHERS THEN DECLARE l_errcode PLS_INTEGER := SQLCODE; BEGIN IF l_errcode = -1 THEN DBMS_OUTPUT.put_line ('Employee ID already in use.'); RAISE; ELSIF l_errcode = THEN DBMS_OUTPUT.put_line ('Invalid department ID: '); RAISE; ELSE null; RAISE; END IF; END; END add_employee; Handling Exception

Jiangping Wang Webster UniversityDistributed Database Applications Combing Multiple Exceptions DECLARE l_department department%ROWTYPE; BEGIN SELECT * INTO l_department FROM department WHERE dept_num = 3; DBMS_OUTPUT.PUT_LINE(l_department.dept_name); EXCEPTION WHEN NO_DATA_FOUND OR TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE('Wrong department number!'); END; /

Jiangping Wang Webster UniversityDistributed Database Applications Named System Exception P. 133 CURSOR_ALREADY_OPEN (ORA-06511) DUP_VAL_ON_INDEX (ORA-00001) INVALID_CURSOR (ORA-01001) INVALID_NUMBER (ORA-01722) NO_DATA_FOUND (ORA-01403) TOO_MANY_ROWS (ORA-01422) VALUE_ERROR (ORA-06502) ZERO_DIVIDE (ORA-01476)

Jiangping Wang Webster UniversityDistributed Database Applications SQLCODE and SQLERRM Built-in Error Functions SQLCODE Return the oracle error number SQLERRM Return the oracle message text associated with the error begin insert into employee values (' ', 'Dave', 'Letterman', NULL, , NULL); exception when others then dbms_output.put_line('code : ' || sqlcode); dbms_output.put_line('message : ' || sqlerrm); if sqlcode = -1 then dbms_output.put_line ('Employee number exists!'); end if; end;

Jiangping Wang Webster UniversityDistributed Database Applications SQLCODE and SQLERRM PROCEDURE DELETE_COMPANY (COMPANY_ID IN NUMBER) IS BEGIN DELETE FROM COMPANY WHERE COMPANY_ID = COMPANY_ID_IN; EXCEPTION WHEN OTHERS THEN DECLARE ERROR_CODE NUMBER := SQLCODE; ERROR_MSG VARCHAR2(512) := SQLERRM; BEGIN IF ERROR_CODE = /*CHILD RECORDS FOUND*/ THEN DELETE FROM EMPLOYEE WHERE COMPANY_ID = COMPANY_ID_IN; DELETE FROM COMPANY WHERE COMPANY_ID = COMPANY_ID_IN; ELSIF ERROR_CODE = /*PARENT KEY NOT FOUND*/ THEN DBMS_OUTPUT.PUT_LINE(‘INVALID COMPANY ID: ’||TO_CHAR(COMPANY_ID_IN)); ELSE DBMS_OUTPUT.PUT_LINE(‘ERROR DELETING COMPANY, ERROR: ’||ERROR_MSG); END IF; END; END DELETE_COMPANY;

Jiangping Wang Webster UniversityDistributed Database Applications DML in PL/SQL INSERT INSERT INTO table VALUES (val1, …); INSERT INTO table AS SELECT …; UPDATE UPDATE table SET col1 = val1 WHERE … DELETE DELETE FROM table WHERE …

Jiangping Wang Cursor attributes for DML operations DML in PL/SQL Webster UniversityDistributed Database Applications

Jiangping Wang Use SQL%FOUND to determine if your DML statement affected any rows DML in PL/SQL Webster UniversityDistributed Database Applications CREATE OR REPLACE PROCEDURE change_author_name ( old_name_in IN books.author%TYPE, new_name_in IN books.author%TYPE, changes_made_out OUT BOOLEAN) IS BEGIN UPDATE books SET author = new_name_in WHERE author = old_name_in; changes_made_out := SQL%FOUND; END;

Jiangping Wang Use SQL%ROWCOUNT when you need to know exactly how many rows were affected by your DML statement DML in PL/SQL Webster UniversityDistributed Database Applications CREATE OR REPLACE PROCEDURE change_author_name ( old_name_in IN books.author%TYPE, new_name_in IN books.author%TYPE, rename_count_out OUT PLS_INTEGER) IS BEGIN UPDATE books SET author = new_name_in WHERE author = old_name_in; rename_count_out := SQL%ROWCOUNT; END;

Jiangping Wang Webster UniversityDistributed Database Applications RETURNING Clause Use returning clause with DML and retrieve information directly into variables Declare l_name employee.lname%type; l_code employee.code%type; Begin for rec in (select * from employee) loop update employee set code = rec.code*1.10 where ssn = rec.ssn returning code, lname into l_code, l_name; dbms_output.put_line ('New code for ' || l_name || ' = ' || l_code); end loop; End; /

Jiangping Wang Webster UniversityDistributed Database Applications Use Records in Insert and Update procedure add_employee(emp_in in employee%rowtype) is begin insert into employee values emp_in; end; declare new_employee employee%rowtype; begin new_employee.ssn := ' '; new_employee.fname := 'Steve'; new_employee.lname := 'Hack'; new_employee.dept_num := '1'; new_employee.code := ; new_employee.sup_ssn := ' '; add_employee(new_employee); new_employee.code := ; update employee set row = new_employee where ssn = new_employee.ssn; end;

Jiangping Wang Using records with the RETURNING clause Use Records in Insert and Update Webster UniversityDistributed Database Applications DECLARE my_book_new_info books%ROWTYPE; my_book_return_info books%ROWTYPE; BEGIN my_book_new_info.isbn := ' '; my_book_new_info.title := 'ORACLE PL/SQL PROGRAMMING'; my_book_new_info.summary := 'General user guide and reference'; my_book_new_info.author := 'FEUERSTEIN, STEVEN AND BILL PRIBYL'; my_book_new_info.page_count := 980; UPDATE books SET ROW = my_book_new_info WHERE isbn = my_book_new_info.isbn RETURNING isbn, title, summary, author, page_count INTO my_book_return_info; END;

Jiangping Wang Webster UniversityDistributed Database Applications Transaction Management Commit – save all outstanding changes Rollback – erase all outstanding changes Savepoint SAVEPOINT savepoint_name; Rollback to savepoint ROLLBACK TO savepoint_name;

Jiangping Wang Webster UniversityDistributed Database Applications Package A package is a grouping or packaging of PL/SQL code elements Package specification Definition or specification of all the publicly available elements in the package Package body Code required to implement elements defined in the package specification

Jiangping Wang Webster UniversityDistributed Database Applications Favorites_pkg Specification Favorites_pkg (p. 625) CREATE OR REPLACE PACKAGE favorites_pkg AUTHID CURRENT_USER IS c_chocolate CONSTANT PLS_INTEGER := 16; c_strawberry CONSTANT PLS_INTEGER := 29; TYPE codes_nt IS TABLE OF INTEGER; my_favorites codes_nt; TYPE fav_info_rct IS REF CURSOR RETURN favorites%ROWTYPE; PROCEDURE show_favorites (list_in IN codes_nt); FUNCTION most_popular RETURN fav_info_rct; END favorites_pkg; /

Jiangping Wang Webster UniversityDistributed Database Applications Favorites_pkg Body Body (p.627) CREATE OR REPLACE PACKAGE BODY favorites_pkg IS g_most_popular PLS_INTEGER := c_strawberry; PROCEDURE show_favorites (list_in IN codes_nt) IS BEGIN FOR indx IN list_in.FIRST.. list_in.LAST LOOP DBMS_OUTPUT.put_line (list_in (indx)); END LOOP; END show_favorites; FUNCTION most_popular RETURN fav_info_rct IS retval fav_info_rct; null_cv fav_info_rct; BEGIN OPEN retval FOR SELECT * FROM favorites WHERE code = g_most_popular; RETURN retval; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN null_cv; END most_popular; END favorites_pkg;

Jiangping Wang Webster UniversityDistributed Database Applications Package Specification CREATE OR REPLACE PACKAGE EMPLOYEE_PKG AS SUBTYPE FULLNAME_T IS VARCHAR2 (200); FUNCTION FULLNAME ( LAST_IN EMPLOYEE.LNAME%TYPE, FIRST_IN EMPLOYEE.FNAME%TYPE) RETURN FULLNAME_T; FUNCTION FULLNAME ( EMPLOYEE_SSN_IN IN EMPLOYEE.SSN%TYPE) RETURN FULLNAME_T; END EMPLOYEE_PKG;

Jiangping Wang Webster UniversityDistributed Database Applications Package Body CREATE OR REPLACE PACKAGE BODY EMPLOYEE_PKG AS FUNCTION FULLNAME ( LAST_IN EMPLOYEE.LNAME%TYPE, FIRST_IN EMPLOYEE.FNAME%TYPE) RETURN FULLNAME_T IS BEGIN RETURN LAST_IN || ', ' || FIRST_IN; END; FUNCTION FULLNAME (EMPLOYEE_SSN_IN IN EMPLOYEE.SSN%TYPE) RETURN FULLNAME_T IS RETVAL FULLNAME_T; BEGIN SELECT FULLNAME (LNAME, FNAME) INTO RETVAL FROM EMPLOYEE WHERE SSN = EMPLOYEE_SSN_IN; RETURN RETVAL; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NULL; WHEN TOO_MANY_ROWS THEN RETURN NULL; END; END EMPLOYEE_PKG;

Jiangping Wang Calling Packaged Elements Package is a container for code elements Run or reference elements in a package From outside of that package, use dot notation package_name.element_name Within the package (specification or body), no need to include the name of the package Webster UniversityDistributed Database Applications

Jiangping Wang Webster UniversityDistributed Database Applications Pets_inc Package CREATE OR REPLACE PACKAGE pets_inc IS max_pets_in_facility CONSTANT INTEGER := 120; pet_is_sick EXCEPTION; CURSOR pet_cur (pet_id_in in pet.id%TYPE) RETURN pet%ROWTYPE; FUNCTION next_pet_shots (pet_id_in IN pet.id%TYPE) RETURN DATE; PROCEDURE set_schedule (pet_id_in IN pet.id%TYPE); END pets_inc;

Jiangping Wang Webster UniversityDistributed Database Applications Pets_inc Package CREATE OR REPLACE PACKAGE BODY pets_inc IS CURSOR pet_cur (pet_id_in IN pet.id%TYPE) RETURN pet%ROWTYPE IS SELECT * FROM pet p WHERE p.id = pet_id_in; FUNCTION next_pet_shots (pet_id_in IN pet.id%TYPE) RETURN DATE /* This Implementation is Just a Stub, Presumably to be Replaced by Something that Does Something Meaningful Later On */ IS BEGIN RETURN SYSDATE; END next_pet_shots; PROCEDURE set_schedule (pet_id_in IN pet.id%TYPE) /* This Implementation is Just a Stub, Presumably to be Replaced by */ /* Something that Does Something Meaningful Later On */ IS BEGIN /* Null is a No-Op. */ /* The Statement is Included to Satisfy the Compile-Time Syntax-Checker */ NULL; END; END pets_inc; /

Jiangping Wang Webster UniversityDistributed Database Applications Pets_inc Package declare c_pet constant pet.id%type:=1099; v_net_appointment date; begin if pets_inc.max_pets_in_facility > 100 then open pets_inc.pet_cur(c_pet); else v_next_appointment:=pets_inc.next_pet_shots(c_pet); end if; exception when pets_inc.pet_is_sick then pets_inc.set_scheducle(c_pet); end;

Jiangping Wang Webster UniversityDistributed Database Applications Package Data Package level data Persist for the session Public and private data State of the packaged cursors persists for the session Open/fetch/close it in different program Close packaged cursor promptly

Jiangping Wang Packaged Cursors Webster UniversityDistributed Database Applications CREATE OR REPLACE PACKAGE book_info IS CURSOR byauthor_cur ( author_in IN books.author%TYPE ) IS SELECT * FROM books WHERE author = author_in; CURSOR bytitle_cur ( title_filter_in IN books.title%TYPE ) RETURN books%ROWTYPE; TYPE author_summary_rt IS RECORD ( author books.author%TYPE, total_page_count PLS_INTEGER, total_book_count PLS_INTEGER); CURSOR summary_cur ( author_in IN books.author%TYPE ) RETURN author_summary_rt; END book_info;

Jiangping Wang Packaged Cursors Webster UniversityDistributed Database Applications CREATE OR REPLACE PACKAGE BODY book_info IS CURSOR bytitle_cur ( title_filter_in IN books.title%TYPE ) RETURN books%ROWTYPE IS SELECT * FROM books WHERE title LIKE UPPER (title_filter_in); CURSOR summary_cur ( author_in IN books.author%TYPE ) RETURN author_summary_rt IS SELECT author, SUM (page_count), COUNT (*) FROM books WHERE author = author_in; END book_info;

Jiangping Wang Packaged Cursors Webster UniversityDistributed Database Applications DECLARE onebook book_info.bytitle_cur%ROWTYPE; BEGIN OPEN book_info.bytitle_cur ('%PL/SQL%'); LOOP EXIT WHEN book_info.bytitle_cur%NOTFOUND; FETCH book_info.bytitle_cur INTO onebook; book_info.display (onebook); END LOOP; CLOSE book_info.bytitle_cur; END;

Jiangping Wang Webster UniversityDistributed Database Applications Use of Packages Encapsulating data manipulation Avoiding the hardcoding of literals Grouping together logically related functionality Caching session-static data to improve application performance

Jiangping Wang Webster UniversityDistributed Database Applications Homework Given the package code and the database table. List the sequence of procedure and function calls initiated for the PL/SQL command. What is the output for the PL/SQL command? Exception handling in procedure. Lab activities