Oracle PL/SQL (2) John Ortiz. Lecture 14Oracle PL/SQL (2)2 Package  A package is a group of related PL/SQL objects (variables, …), procedures, and functions.

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

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.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Chapter 4B: More Advanced PL/SQL Programming
Creating Triggers.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
Programming in Oracle with PL/SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Stored Procedures PL/SQL code stored in the database and executed when called by the user. Called by procedure name from another PL/SQL block or using.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
PL/SQL and the Table API. Benefits of Server-Side Code Speedy Pizza MENU NAPOLITAINE PIZZA Reduced network traffic Maintainability Data integrity Triggers.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
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:
PL/SQL PL/SQL is Oracle's Procedural Language extension to SQL. PL/SQL has many programming language features. Program units written in PL/SQL can be stored.
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.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
PL/SQL A BRIEF OVERVIEW DAVID WILSON. PL/SQL User’s Guide and Reference PL/SQL User’s Guide and Reference.
PL/SQL ISYS 464. PL/SQL Block-Structured Variable declaration section Executable section Exception-handling section.
In Oracle.  A PL/SQL block stored in the database and fired in response to a specified event ◦ DML statements : insert, update, delete ◦ DDL statements.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
Advanced SQL: Cursors & Stored Procedures
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
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,
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
implicit and an explicit cursor
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
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.
A database trigger is a stored PL/SQL program unit associated with a specific database table. ORACLE executes (fires) a database trigger automatically.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Database Management COP4540, SCS, FIU Database Trigger.
ADVANCED FEATURES Of PL/SQL. * PROCEDURAL LANGUAGE FEATURE * OBJECT ORIENTED FEATURES * EXCEPTION HANDLING * INCREASED SECUTIRY * PACKAGES.
Chapter 8 Advanced SQL Pearson Education © Chapter 8 - Objectives How to use the SQL programming language How to use SQL cursors How to create stored.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
Creating Database Triggers
PL/SQL.
SQL Stored Triggers Presented by: Dr. Samir Tartir
PL/SQL Package Week 8.
Instructor: Mohamed Eltabakh
Database Processing: David M. Kroenke’s Chapter Seven:
Introduction to Triggers
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 7 Using SQL in Applications
Information Management
TRIGGERS.
Presentation transcript:

Oracle PL/SQL (2) John Ortiz

Lecture 14Oracle PL/SQL (2)2 Package  A package is a group of related PL/SQL objects (variables, …), procedures, and functions.  Each package definition has two parts:  package specification  package body  Package specification provides an interface to the users of the package.  Package body contains the actual code.

Lecture 14Oracle PL/SQL (2)3 Package Specification create or replace package banking as function check_balance (account_no in Accounts.acct_no%type) return Accounts.balance%type; procedure deposit (account_no in Accounts.acct_no%type, amount in Accounts.balance%type); procedure withdraw (account_no in Accounts.acct_no%type, amount in Accounts.balance%type); end; / show errors

Lecture 14Oracle PL/SQL (2)4 Package Body create or replace package body banking as function check_balance (account_no in Accounts.acct_no%type) return Accounts.balance%type is acct_balance Accounts.balance%type; begin select balance into acct_balance from Accounts where acct_no = account_no; return acct_balance; end;

Lecture 14Oracle PL/SQL (2)5 Package Body (cont.) procedure deposit (account_no in Accounts.acct_no%type, amount in Accounts.balance%type) is begin if (amount <= 0) then dbms_output.put_line(‘Wrong amount.’); else update Accounts set balance = balance + amount where acct_no = account_no; end if; end;

Lecture 14Oracle PL/SQL (2)6 Package Body (cont.) procedure withdraw (account_no in Accounts.acct_no%type, amount in Accounts.balance%type) is acct_balance Accounts.balance%type; begin acct_balance := check_balance(account_no); if (amount > acct_balance) then dbms_output.put_line(‘Insufficient fund.’);

Lecture 14Oracle PL/SQL (2)7 Package Body (cont.) else update Accounts set balance = balance - amount where acct_no = account_no; end if; end; end; /* end of the package body */ / show errors

Lecture 14Oracle PL/SQL (2)8 Public versus Private Constructs  Any construct listed in package specification is public and accessible to anyone else.  Constructs listed in package body but not in package specification are private and accessible only to other constructs in the same package.

Lecture 14Oracle PL/SQL (2)9 Use of Package  Objects defined in a package specification can be used in other PL/SQL packages, blocks and SQL queries. Package_Name.Object  Compile the package  SQL> start packspec1  SQL> start packbody1  May use execute to invoke a package. SQL> execute banking.deposit(100, 200); SQL> execute banking.withdraw(200, 500);

Lecture 14Oracle PL/SQL (2)10 Use of a Stored Function  Declare a variable SQL> var bal number  Execute the function SQL> execute :bal := banking.check_balance(200);  Must precede variable with a colon “:”.  Print the value SQL> print bal

Lecture 14Oracle PL/SQL (2)11 Find About Packages  What packages do we have? select object_name, object_type, created from user_objects where object_type = ‘PACKAGE’;  What code is in the package? select text from user_source where name = ‘BANKING’;  Both specification and body will be shown.

Lecture 14Oracle PL/SQL (2)12 Cursor  A construct to access query result one tuple at a time.  Define a cursor: cursor c1 is select cid, cname, city from customers;  Working with a cursor:  open cursor_name;  fetch cursor_name into record or variables;  close cursor_name;

Lecture 14Oracle PL/SQL (2)13 An Example of Cursors declare cursor c1 is select cid, cname, city from customers; c1_rec c1%rowtype; begin open c1; fetch c1 into c1_rec; dbms_output.put_line(c1_rec.cid || ‘, ‘ || c1_rec.cname || ‘, ‘ || c1_rec.city); close c1; end; /

Lecture 14Oracle PL/SQL (2)14 Cursor Attributes begin if (not c1%isopen) then /* attribute */ open c1; end if; fetch c1 into c1_rec; while c1%found loop /* attribute */ dbms_output.put_line(c1_rec.cid || ‘, ‘ || c1_rec.cname || ‘, ‘ || c1_rec.city); fetch c1 into c1_rec; end loop; close c1; end; /

Lecture 14Oracle PL/SQL (2)15 Cursor For Loop begin for c1_record in c1 loop if (c1_record.city = ‘New York’) then dbms_output.put_line(c1_rec.cid || ‘, ‘ || c1_rec.cname || ‘, ‘ || c1_rec.city); end if; end loop; end; /  open, fetch and close are done implicitly.  No need to declare c1_record.

Lecture 14Oracle PL/SQL (2)16 Cursor for Update  Declare a cursor for update cursor c1 is select cid, cname, city from customers for update;  Use the current tuple in a cursor. update customers set city = ‘Boston’ where current of c1; delete from customers where current of c1;

Lecture 14Oracle PL/SQL (2)17 Cursor for Update declare cursor c1 is select * from customers for update; begin for c1_rec in c1 loop if (c1_rec.city = ‘New York’) then delete from customers where current of c1; end if; end loop; end;

Lecture 14Oracle PL/SQL (2)18 Parameterized Cursor  Declaration: cursor c1(d_name in Students.dept_name%type) is select age, avg(GPA) from Students where dept_name = d_name group by age;  Usage: open c1(‘Computer Science’);

Lecture 14Oracle PL/SQL (2)19 Built-in Package: dbms_output  dbms_output is primarily used to help debugging PL/SQL programs.  Has following public procedures:  dbms_output.disable.  dbms_output.enable(20000).  Set buffer size to 20,000 bytes (default is 2000)  put_line( ) & put( ).  new_line.  Get_line(line, status). (status=0 or 1)

Lecture 14Oracle PL/SQL (2)20 Trigger  A trigger is an event-condition-action rule coded in PL/SQL and is useful for enforcing various integrity constraints and business rules.  An event is an update operation: insertion, deletion or update.  The action can be a set of additional update operations or other PL/SQL statements.  A trigger fires (executes the action) at a time before or after an event occurs and additional condition are satisfied.

Lecture 14Oracle PL/SQL (2)21 A Sample Trigger create or replace trigger raise_sal before update of salary on employees for each row when (new.salary > old.salary * 1.2) begin dbms_output.put_line(‘Old salary is ‘ || :old.salary || ‘, ‘ || ‘New salary is ‘ || :new.salary); dbms_output.put_line(‘The raise is too high!’); end;

Lecture 14Oracle PL/SQL (2)22 Row Trigger  Row Trigger  Fire once for each row that is affected by the event and satisfies the additional condition in the when clause.  Must specify for each row.  Predefined references: new & old  new is applicable for insert and update  old is applicable for update and delete  use :new.salary & :old.salary in trigger body

Lecture 14Oracle PL/SQL (2)23 Trigger Applications  Add a log entry each time the price of a product is changed.  The log table: products_log (pid, username, update_date, old_price, new_price);

Lecture 14Oracle PL/SQL (2)24 Trigger Application (cont.)  Create a trigger: create or replace trigger update_p_price after update of price on products for each row begin insert into products_log values (:old.pid, user, sysdate, :old.price, :new.price); end;

Lecture 14Oracle PL/SQL (2)25 Another Trigger Application  If a student is removed, delete all enrollments by the student. create or replace trigger stud_enroll after delete on students for each row begin delete from enrollments where sid = :old.sid; end;

Lecture 14Oracle PL/SQL (2)26 Look Ahead  Next topic: Embedded SQL  Reading textbook:  Oracle 8, Chapter 3.