More and Still More on Procedures and Functions

Slides:



Advertisements
Similar presentations
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
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.
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.
Relational example using donor, donation and drive tables For additional information see the speaker notes!
PL/SQL - Using IF statements Please use speaker notes for additional information!
Chapter 8 Embedded SQL.
More on IF statements Use speaker notes for additional information!
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
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.
Table maintenance revisited (again) Please use speaker notes for additional information!
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
SQL Use of Functions Character functions Please use speaker notes for additional information!
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
PHP meets MySQL.
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
University of Sunderland COM 220 Lecture Six Slide 1 Building Interactive Forms Applications using Oracle.
Array - adding to array at run time Please see speaker notes for additional information!
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
Examples dealing with cursors and tables Please use speaker notes for additional information!
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
Manipulating data within PL/SQL Please use speaker notes for additional information!
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.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Exceptions in PL/SQL Please use speaker notes for additional information!
Introduction to Oracle - SQL Additional information is available in speaker notes!
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
implicit and an explicit cursor
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Oracle PL/SQL Loops Please use speaker notes for additional information!
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Introduction to PL/SQL As usual, use speaker notes for additional information!
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
RETRIEVE A NO. OF ROWS ¦ Declare a cursor ¦ Open the cursor ¦ Fetch rows of data ¦ Stop fetching rows ¦ Close the cursor.
More about maintaining a table Use speaker notes for additional information!
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
ASP.NET Programming with C# and SQL Server First Edition
Chapter 2 Anonymous Block
A Guide to SQL, Seventh Edition
Data Virtualization Tutorial: Introduction to SQL Script
Difference between Oracle PL/SQL and MySQL
PL/SQL.
Introduction to Triggers
More on Procedures (Internal/Local procedures)
Handling Exceptions.
SQL PL/SQL Presented by: Dr. Samir Tartir
Error Handling Summary of the next few pages: Error Handling Cursors.
Introduction to Procedures
MySQL - Creating donorof database offline
Arrays & Functions Lesson xx
Introduction to Functions
Advanced PL/SQL Programing
Please use speaker notes for additional information!
Sa0951a PL/SQL 1: Introduction
Handling Exceptions.
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
PRACTICE OVERVIEW PL/SQL Part - 1.
Using screens and adding two numbers - addda.cbl
Lets Play with arrays Singh Tripty
More on If statements (Calculate, Calculate1, Calculate2)
Presentation transcript:

More and Still More on Procedures and Functions Please use speaker notes for additional information! This is only an introduction to Procedure and Functions. Details would be appropriate for a more advanced course.

More on Procedures and Functions SET VERIFY OFF DECLARE v_idno new_donation.idno%TYPE :='&input_idno'; v_driveno new_donation.driveno%TYPE :='&input_driveno'; v_contamt new_donation.contamt%TYPE :=&input_contamt; v_newcontamt new_donation.contamt%TYPE; v_code VARCHAR2(2); FUNCTION CalcNewDon (f_code VARCHAR2, f_contamt NUMBER) RETURN NUMBER IS f_newcontamt new_donation.contamt%TYPE; BEGIN IF f_code = 'EX' THEN f_newcontamt := f_contamt * 1.3; ELSE IF f_code = 'VG' THEN f_newcontamt := f_contamt * 1.2; IF f_code = 'OK' THEN f_newcontamt := f_contamt * 1.1; f_newcontamt := f_contamt; END IF; RETURN f_newcontamt; END CalcNewDon; PROCEDURE AddDonProc (p_idno new_donation.idno%TYPE, p_driveno new_donation.driveno%TYPE, p_contamt new_donation.contamt%TYPE) AS INSERT INTO new_donation(idno, driveno, contamt) VALUES(p_idno, p_driveno, p_contamt); END AddDonProc; Data passed to the function. Result of the function - return number. The internal function CalcNewDon will return a calculated amount in the variable f_newcontamt. The amount returns depends on the function code that is passed to the function when it is executed. The procedure AddDonProc receives the identification number, the drive number, and the contribution amount when the procedure is called. This information is transferred over and stored in p_idno, p_driveno and p_contamt and then used to INSERT a line in the table. This slide will start to look at PL/SQL code that has both an internal procedure and an internal function to try and clarify the differences. The examples are from the notes! This shows the DECLARE part of the PL/SQL code. The next slide will show the BEGIN. Note that the name in the END statement is not required - it can help to clarify when there are multiple procedures and functions.

More on Procedures and Functions The data passed goes to f_code and f_contamt. BEGIN IF v_contamt > 500 THEN v_code := 'EX'; v_newcontamt := CalcNewDon(v_code, v_contamt); ELSE IF v_contamt > 250 THEN v_code := 'VG'; IF v_contamt > 100 THEN v_code := 'OK'; v_code := 'NG'; END IF; AddDonProc (v_idno, v_driveno, v_newcontamt); END; / SET VERIFY ON F_newcontamt comes back and is known here as v_newcontamt. The processing starts with the BEGIN on this page. Depending on the contribution amount that is inputed by the user, a code is set and the function is passed the code and the inputed amount. The results of the calculation in the function will be returned as f_contamt and stored in v_newcontamt. After the function has returned the result. The AddDonProc is called and is passed the information needed to write a record. The information passed includes the calculated amount from the function. The procedure then INSERTS a new record and the processing is complete. The data passed goes to p_idno, p_driveno and p_contamt.

More on Procedures and Functions SQL> @ call_adddonproc6 Enter value for input_idno: 11111 Enter value for input_driveno: 100 Enter value for input_contamt: 700 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 25 12121 200 23-FEB-99 40 23456 100 03-MAR-99 20 33333 300 10-MAR-99 10 22222 100 14-MAR-99 10 12121 100 04-JUN-99 50 11111 200 12-JUN-99 35 23456 300 14-JUN-99 10 12121 300 10-JUN-99 75 12121 100 100 11111 100 910 11 rows selected. The input is 11111, 100 and 700. Since the inputted contribution amount is greater than 700 the code EX is passed to the function along with the amount. In the function, if the code is EX then the contribution amount is multiplied by 1.3 (700 * 1.3 = 910). The result of 910 is returned. Then the identification number of 11111, the drive number of 100 and the new contribution amount of 910 is passed to the procedure which inserts the record as shown. Note that the processing begins with the BEGIN of call_adddonproc6. This anonymous block has the code to get the calculation result from the function and call the procedure to do the INSERT. Notice that the Insert made 11 rows - originally there were 10.

More on Procedures and Functions SQL> @ call_adddonproc6 Enter value for input_idno: 22222 Enter value for input_driveno: 200 Enter value for input_contamt: 300 PL/SQL procedure successfully completed. SQL> SELECT * FROM new_donation; IDNO DRI CONTDATE CONTAMT ----- --- --------- --------- 11111 100 07-JAN-99 25 12121 200 23-FEB-99 40 23456 100 03-MAR-99 20 33333 300 10-MAR-99 10 22222 100 14-MAR-99 10 12121 100 04-JUN-99 50 11111 200 12-JUN-99 35 23456 300 14-JUN-99 10 12121 300 10-JUN-99 75 12121 100 100 11111 100 910 22222 200 360 12 rows selected. In this example, the input is 22222, 200, and 300. The contribution amount is not greaer than 500, but it is greater than 250 so the code VG is passed to the function along with the amount. In the function the code VG causes the contribution amount to be multiplied by 1.2 (300 times 1.2 = 360) The result is returned and then passed to the procedure along with the identification number and the drive number to be inserted into the table. The inserted row brings the total from 11 to 12.

Still More - Another example of internal procedures and functions SQL> DESC testprfn; Name Null? Type ------------------------------- -------- ---- FLD1 NUMBER(3) FLD2 VARCHAR2(10) FLD3 NUMBER(5) FLD4 VARCHAR2(10) SQL> SELECT * FROM testprfn; FLD1 FLD2 FLD3 FLD4 --------- ---------- --------- ---------- 123 Boston 12345 John 234 Providence 23456 Ann 345 Fall River 34567 Linda 456 Boston 45678 Lawrence 567 Fall River 56789 Susan Notice that there is a testprfn and a testprfn2 which have the same layout and data.

Still More - Another example of internal procedures and functions BEGIN OPEN fld_cursor; FETCH fld_cursor into v_fld1, v_fld2, v_fld3, v_fld4; WHILE fld_cursor%FOUND LOOP v_fldtot := calc_num(v_fld1, v_fld3); change_fld4(v_fldtot, v_fld4); UPDATE testprfn2 SET fld4 = v_fld4 WHERE CURRENT of fld_cursor; dbms_output.put_line(v_fld1||' '||v_fld4||' '||v_fldtot); END LOOP; CLOSE fld_cursor; END; / SET SERVEROUTPUT OFF SET SERVEROUTPUT ON DECLARE v_fld1 testprfn2.fld1%TYPE; v_fld2 testprfn2.fld2%TYPE; v_fld3 testprfn2.fld3%TYPE; v_fld4 testprfn2.fld4%TYPE; v_fldtot testprfn2.fld4%TYPE; CURSOR fld_cursor IS SELECT * FROM testprfn2 FOR UPDATE; PROCEDURE change_fld4 (p_fldtot IN testprfn2.fld1%TYPE, p_fld4 IN OUT testprfn2.fld4%TYPE) AS BEGIN IF p_fldtot > 30000 AND LENGTH (p_fld4) < 8 THEN p_fld4 := ('*'||p_fld4||'*'); END IF; END change_fld4; FUNCTION calc_num (f_fld1 NUMBER, f_fld3 NUMBER) RETURN NUMBER IS f_fldtot testprfn2.fld4%TYPE; f_fldtot := f_fld3 - f_fld1; RETURN f_fldtot; END calc_num; Variables that will be filled from the FETCH. Variable that will be used to hold the results from the function. Cursor that will be filled when it is opened and will hold the data from testprfn2. Function that will return receive the contents of field 1 and field 3 and return the results of a subtraction. Procedure that will test the total returned from the function and the length of field 4 to determine if the contents of field 4 should be changed. Main code (anonymous block) that handles the cursor, the function, the procedure - all of the processing. The begin of the main anonymous block starts the processing. It opens the cursor which fills it according to the SELECT. It then Fetches the first record and starts the loop. The function is executed and the result of the calculation is returned. Then the procedure is called and passed the result of the function and another piece of data. After the function, the file is updated. Notice it is updated even if no change has been made (not very efficient) - a code could have been passed back from the function to determine if the update should take place. The results are displayed and the next record is fetched and the loop is continued until all records in the cursor have been processed. For a more detailed analysis, please see the notes.

Still More - Another example of internal procedures and functions SQL> edit procfnc2 This line brought up the editor with the code shown on the previous slide. SQL> @ procfnc2 123 John 12222 234 Ann 23222 345 *Linda* 34222 456 Lawrence 45222 567 *Susan* 56222 PL/SQL procedure successfully completed. SQL> select * from testprfn2; FLD1 FLD2 FLD3 FLD4 --------- ---------- --------- ---------- 123 Boston 12345 John 234 Providence 23456 Ann 345 Fall River 34567 *Linda* 456 Boston 45678 Lawrence 567 Fall River 56789 *Susan* Results of displaying the processing on the screen. Note that where the total (field 3 -field1) was greater than 30000 and the name was less than 8 the results are displayed. Update file. Note that the third and fifth records were updated. For details on how results, see the next slide.

Still More - Another example of internal procedures and functions fld3 - fld1 = 12345 - 123 = 12222 which is not > 30000 so even though the length of fld4 is less than 8 the change is not made. SQL> select * from testprfn2; FLD1 FLD2 FLD3 FLD4 --------- ---------- --------- ---------- 123 Boston 12345 John 234 Providence 23456 Ann 345 Fall River 34567 *Linda* 456 Boston 45678 Lawrence 567 Fall River 56789 *Susan* fld3 - fld1 = 23456 - 234 = 23222 which is not > 30000 so even though the length of fld4 is less than 8 the change is not made. fld3 - fld1 = 34567 - 345 = 34222 which is greater than 30000 and the length of fld4 is less than 8 so the change is made. fld3 - fld1 = 45678 - 456 = 45222 which is greater than 30000 but the length of fld4 is equal to 8 so the change is not made. This shows the results of the processing that is done in the function and in the procedure. The function does the calculation and the procedure does the checking. fld3 - fld1 = 56789 - 567 = 56222 which is greater than 30000 and the length of fld4 is less than 8 so the change is made.