PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.

Slides:



Advertisements
Similar presentations
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
Advertisements

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.
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.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Introduction to Oracle9i: SQL1 Views. Introduction to Oracle9i: SQL2 Chapter Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE.
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Bordoloi and Bock PROCEDURES, FUNCTIONS & TRIGGERS.
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:
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 11 Introduction to Dynamic SQL and Object Technology.
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.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 6 Functions.
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.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
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.
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
Commercial RDBMSs: Office Access and Oracle Pertemuan 13 Matakuliah: M0564 /Pengantar Sistem Basis Data Tahun : 2008.
Chapter 13Introduction to Oracle9i: SQL1 Chapter 13 User Creation and Management.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
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.
PRACTICE OVERVIEW PL/SQL Part Your stored procedure, GET_BUDGET, has a logic problem and must be modified. The script that contains the procedure.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Creating Stored Functions
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Chapter 8 Dependencies, Privileges and Compilation Oracle11g:
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Oracle9i Developer: PL/SQL Programming Chapter 3 PL/SQL Processing.
UNIT - V STORED PROCEDURE.
PL/SQL Scripting in Oracle:
PRACTICE OVERVIEW PL/SQL Part - 2.
Database Management Systems 2
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
Chapter 10 Oracle9i Developer: PL/SQL Programming
Chapter 1 Introduction to PL/SQL Oracle10g Developer:
Creating Noninput Items
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
PRACTICE OVERVIEW PL/SQL Part - 1.
Prof. Arfaoui. COM390 Chapter 9
Procedures Oracle & MySQL
Prof. Arfaoui. COM390 Chapter 6
Handling Data in PL/SQL Blocks
Prof. Arfaoui. COM390 Chapter 7
Presentation transcript:

PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions

PL/SQLPL/SQL Oracle11g: PL/SQL Programming2 Chapter Objectives After completing this lesson, you should be able to understand: –Functions –Creating a stored function –Using OUT parameters in functions –Including multiple RETURN statements in a function –Using a RETURN statement in a procedure –Using constraints of actual and formal parameters

PL/SQLPL/SQL Oracle11g: PL/SQL Programming3 Chapter Objectives (continued) After completing this lesson, you should be able to understand (continued): –Understanding and controlling how parameter values are passed –Working with function purity levels –Additional program unit options –Referencing the data dictionary for program units –Deleting program units

PL/SQLPL/SQL Oracle11g: PL/SQL Programming4 Brewbean’s Challenge Need program module to check a user login

PL/SQLPL/SQL Oracle11g: PL/SQL Programming5 Brewbean’s Challenge (continued) Need program module to calculate shipping cost based on the number of items in the basket

PL/SQLPL/SQL Oracle11g: PL/SQL Programming6 Introduction to Functions A function is similar to a procedure in that it can accomplish a task and retrieve/return values A function is part of an expression, not an entire statement such as a procedure Can be used in both PL/SQL and SQL statements Same as Oracle-supplied functions (ROUND, TO_CHAR) Contains a RETURN statement

PL/SQLPL/SQL Oracle11g: PL/SQL Programming7 Example of Oracle-Supplied Function SQL SELECT idProduct, price, ROUND(price, 0) FROM bb_product WHERE idProduct < 4; PL/SQL DECLARE v_amt1 number(5,2); v_amt2 number(3,0); BEGIN v_amt1 := 32.50; v_amt2 := ROUND(v_amt1,0); DBMS_OUTPUT.PUT_LINE(v_amt2); END;

PL/SQLPL/SQL Oracle11g: PL/SQL Programming8 Function Create Statement

PL/SQLPL/SQL Oracle11g: PL/SQL Programming9 Function Example

PL/SQLPL/SQL Oracle11g: PL/SQL Programming10 Invoking a Function from a Block An assignment statement is used – a function RETURNS a value!

PL/SQLPL/SQL Oracle11g: PL/SQL Programming11 Attempt to Invoke Stand-alone

PL/SQLPL/SQL Oracle11g: PL/SQL Programming12 Use Function in SQL

PL/SQLPL/SQL Oracle11g: PL/SQL Programming13 Brewbean’s Member Display CREATE OR REPLACE FUNCTION memfmt1_sf (p_id IN NUMBER, p_first IN VARCHAR2, p_last IN VARCHAR2) RETURN VARCHAR2 IS lv_mem_txt VARCHAR2(35); BEGIN lv_mem_txt := 'Member ' || p_id || ' - ' || p_first || ' ' || p_last; RETURN lv_mem_txt; END;

PL/SQLPL/SQL Oracle11g: PL/SQL Programming14 Member Display Test

PL/SQLPL/SQL Oracle11g: PL/SQL Programming15 Using OUT Mode in a Function OUT parameters are not typically used in functions, as: –Mixing OUT and RETURN values can lead to confusion –It prohibits the function from being used in SQL

PL/SQLPL/SQL Oracle11g: PL/SQL Programming16 Multiple RETURN Statements Note: Only one RETURN statement can execute

PL/SQLPL/SQL Oracle11g: PL/SQL Programming17 RETURN Statement in a Procedure Different purpose than a RETURN statement in a function Used to change flow of execution Stops processing in that block and moves to the next statement after the procedure call Stand-alone statement with no arguments

PL/SQLPL/SQL Oracle11g: PL/SQL Programming18 Parameter Constraints Formal parameters – included in a program unit Actual parameters – arguments used in a program unit call Argument for an OUT parameter must be a variable to hold the value returned Actual parameters determine the size of the formal parameters

PL/SQLPL/SQL Oracle11g: PL/SQL Programming19 Passing Parameter Values Two techniques used to pass values between actual and formal parameters: 1.Passed by Reference – create pointer to value in the actual parameter 2.Passed by Value – copies value from actual to formal parameter Pass by value is the default Use a compiler hint to use pass by reference

PL/SQLPL/SQL Oracle11g: PL/SQL Programming20 Pass by Reference

PL/SQLPL/SQL Oracle11g: PL/SQL Programming21 Purity Levels Restrictions on functions used in SQL –Functions cannot modify any tables in Oracle8 and prior versions Beginning with Oracle8i, the function cannot modify a table used in the SQL statement that calls the function; however, it may alter other tables if called from a non-select statement –If used in a remote or parallel operation, no reading or writing of packaged variables allowed –If used in a SELECT, VALUES, or SET clause, the function can write values to packaged variables; otherwise, it is not allowed

PL/SQLPL/SQL Oracle11g: PL/SQL Programming22 Purity Levels (continued) Restrictions on functions used in SQL (continued) –Functions cannot be used in a check constraint or as a default value of a table column –If the function calls other subprograms, the subprograms cannot break these rules –Must be a stored database object (or in a stored package) –Can use only IN parameters –Must be a row function (not a group function)

PL/SQLPL/SQL Purity Levels (continued) Restrictions on functions used in SQL (continued) –Formal parameter data types must use database data types (no PL/SQL data types such as BOOLEAN are permitted) –Return data types must be a database data type –Must not issue transaction control statements to end the current transaction prior to execution –Cannot issue ALTER SESSION or ALTER SYSTEM commands Oracle11g: PL/SQL Programming23

PL/SQLPL/SQL Oracle11g: PL/SQL Programming24 Purity Levels (continued) Level AcronymLevel NameLevel Description WNDSWrites No Database State Function does not modify any database tables (No DML) RNDSReads No Database State Function does not read any tables (No select) WNPSWrites No Package State Function does not modify any packaged variables (packaged variables are variables declared in a package specification; they are discussed in detail in Chapter 6) RNPSReads No Package State Function does not read any packaged variables

PL/SQLPL/SQL Oracle11g: PL/SQL Programming25 Purity Levels Test Function that updates table bb_test1 CREATE OR REPLACE FUNCTION fct_test1_sf (p_num IN NUMBER) RETURN NUMBER IS BEGIN UPDATE bb_test1 SET col1 = p_num; RETURN p_num; END;

PL/SQLPL/SQL Oracle11g: PL/SQL Programming26 Purity Levels Test

PL/SQLPL/SQL Oracle11g: PL/SQL Programming27 Additional Options

PL/SQLPL/SQL Oracle11g: PL/SQL Programming28 Data Dictionary Information DESCRIBE identifies parameters and return value data type

PL/SQLPL/SQL Oracle11g: PL/SQL Programming29 Data Dictionary Information (continued) View source code using USER_SOURCE

PL/SQLPL/SQL Oracle11g: PL/SQL Programming30 Delete Functions DROP FUNCTION function_name;

PL/SQLPL/SQL Oracle11g: PL/SQL Programming31 Summary Functions can be used in PL/SQL and SQL statements A function is part of an expression Functions include parameters and must return a value OUT parameter rarely used Pass parameter values by value or reference Multiple RETURN statements can be included and only one is executed

PL/SQLPL/SQL Summary (continued) Actual versus formal parameters –Formal parameters – included in a program unit –Actual parameters – arguments used in a program unit call Purity levels refer to rules for functions to be used in SQL statements Options are available for improving performance such as PARALLEL_ENABLE DESCRIBE and USER_SOURCE view DROP command removed a function Oracle11g: PL/SQL Programming32