25
Copyright Ó Oracle Corporation, 1998. All rights reserved. 4 Creating Functions

Creating Functions

  • Upload
    mirit

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

Creating Functions. Objectives. After completing this lesson, you should be able to do the following: Describe the uses of functions Create client-side and server-side functions Invoke a function Remove a function Differentiate between a procedure and a function. - PowerPoint PPT Presentation

Citation preview

Page 1: Creating Functions

Copyright Ó Oracle Corporation, 1998. All rights reserved.

4Creating Functions

Page 2: Creating Functions

4-2 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Objectives

After completing this lesson, you should be able to do the following:• Describe the uses of functions• Create client-side and server-side

functions• Invoke a function• Remove a function• Differentiate between a procedure and a

function

Page 3: Creating Functions

4-3 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Overview of Stored Functions

• A function is a named PL/SQL block that returns a value.

• A function can be stored in the database, as a database object, for repeated execution.

• A function can be called as part of an expression.

Page 4: Creating Functions

4-4 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Syntax for Creating FunctionsCREATE [OR REPLACE] FUNCTION function_name (argument1 [mode1] datatype1, argument2 [mode2] datatype2, . . .)RETURN datatypeIS|ASPL/SQL Block;

The PL/SQL block must have at least one RETURN statement.

Page 5: Creating Functions

4-5 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Creating a Function

SQL*Plus CodeOracle

Procedure Builder

CodeEditor

1

SQL> START file.sqlSave 2

Source code

Execute

p-code

Compile

Oracle

Page 6: Creating Functions

4-6 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Creating a Stored Function Using SQL*Plus

1. Enter the text of the CREATE FUNCTION statement in a system editor or word processor and save it as a script file (.sql extension).

2. From SQL*Plus, run the script file to compile the source code into p-code and store both in the database.

3. Invoke the function from an Oracle Server environment to determine

whether it executes without error.

Page 7: Creating Functions

4-7 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Creating a Stored Function Using SQL*Plus: Example

SQL> CREATE OR REPLACE FUNCTION get_sal 2 (v_id IN emp.empno%TYPE) 3 RETURN NUMBER 4 IS 5 v_salary emp.sal%TYPE :=0; 6 BEGIN 7 SELECT sal 8 INTO v_salary 9 FROM emp 10 WHERE empno = v_id; 11 RETURN (v_salary); 12 END get_sal; 13 /

Page 8: Creating Functions

4-8 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Creating a Function Using Procedure Builder

Procedure Builder allows you to:• Create a client-side function• Create a server-side function• Drag and drop functions between client

and server

Page 9: Creating Functions

4-9 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Creating Functions Using Procedure Builder: Example

Return the tax based on a specified value.FUNCTION tax (v_value IN NUMBER) RETURN NUMBERISBEGIN RETURN (v_value * .08);END tax;

Page 10: Creating Functions

4-10 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Executing Functions

• Invoke a function as part of a PL/SQL expression.

• Create a host variable to hold the returned value.

• Execute the function. The host variable will be populated by the RETURN value.

Page 11: Creating Functions

4-11 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Executing Functions in SQL*Plus: Example

Calling environment GET_SAL functionv_id7934

RETURN v_salary

SQL> START get_salary.sqlFunction created.SQL> VARIABLE g_salary numberSQL> EXECUTE :g_salary := get_sal(7934)PL/SQL procedure successfully completed.

SQL> PRINT g_salary G_SALARY

------------------ 1300

Page 12: Creating Functions

4-12 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Executing Functions in Procedure Builder: Example

Display the tax based on a specified value.PL/SQL> .CREATE NUMBER x PRECISION 4PL/SQL> :x := tax(1000);PL/SQL> TEXT_IO.PUT_LINE (TO_CHAR(:x));80

Calling environment TAX function

v_value1000

RETURN (computed value)

Page 13: Creating Functions

4-13 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Advantages of User-Defined Functions in SQL Expressions

• Extend SQL where activities are too complex, too awkward, or unavailable with SQL

• Can increase efficiency, by using them in the WHERE clause to filter data, as opposed to filtering the data in the application

• Can manipulate character strings

Page 14: Creating Functions

4-14 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Locations to Call User-Defined Functions

• Select list of a SELECT command• Condition of the WHERE and HAVING

clauses• CONNECT BY, START WITH, ORDER BY,

and GROUP BY clauses• VALUES clauses of the INSERT

command• SET clause of the UPDATE command

Page 15: Creating Functions

4-15 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Calling Functions from SQL Expressions: Restrictions

• A user-defined function must be a stored function.

• A user-defined function must be a ROW function, not a GROUP function.

• A user-defined function only takes IN parameters, not OUT, or IN OUT.

• Datatypes must be valid SQL datatypes, CHAR, DATE, or NUMBER.

• Datatypes cannot be PL/SQL types such as BOOLEAN, RECORD, or TABLE.

Page 16: Creating Functions

4-16 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Calling Functions from SQL Expressions: Restrictions

• INSERT, UPDATE, or DELETE commands are not allowed.

• Calls to subprograms that break the above restriction are not allowed.

Page 17: Creating Functions

4-17 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Removing Functions

• Using SQL*Plus:Drop a server-side function.

• Using Procedure Builder:– Drop a server-side function.– Delete a client-side function.

Page 18: Creating Functions

4-18 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Removing a Server-SideFunction

Using SQL*Plus• Syntax

• ExampleDROP FUNCTION function_name

SQL> DROP FUNCTION get_sal;Function dropped.

Page 19: Creating Functions

4-19 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Removing Server-Side Functions

Using Procedure Builder1. Connect to the database.2. Expand the Database Objects node.3. Expand the schema of the owner of the function.4. Expand the Stored Program Units node.5. Click the function you want to drop.6. Click Delete in the Object Navigator.7. Click Yes to confirm.

Page 20: Creating Functions

4-20 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Removing a Client-Side Function

Using Procedure Builder1. Expand the Program Units node.2. Click the function you want to

remove.3. Click Delete in the Object Navigator.4. Click Yes to confirm.

Page 21: Creating Functions

4-21 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Procedure or Function?

Procedure

(DECLARE)

BEGIN

EXCEPTION

END;

IN argumentOUT argument IN OUT argument

CallingEnvironment

CallingEnvironment

Function

(DECLARE)

BEGIN

EXCEPTION

END;

IN argument

Page 22: Creating Functions

4-22 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Comparing Procedures and Functions

ProcedureExecute as a PL/SQL statementNo RETURN datatype

Can return none, one or many values

FunctionInvoke as part of an expressionMust contain a RETURN datatypeMust return a single value

Page 23: Creating Functions

4-23 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Benefits of Stored Procedures and Functions

• Improved performance• Improved maintenance• Improved data security and integrity

Page 24: Creating Functions

4-24 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Summary

• A function is a named PL/SQL block that must return a value.

• A function is invoked as part of an expression.

• A stored function can be called in SQL statements.

Page 25: Creating Functions

4-25 Copyright Ó Oracle Corporation, 1998. All rights reserved.

Practice Overview

• Creating stored functions• Invoking a stored function from a SQL

statement• Invoking a stored function from a stored

procedure