Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / SQL reference.pdf
Скачиваний:
24
Добавлен:
12.05.2015
Размер:
11.92 Mб
Скачать

User-Defined Functions

Table 6–4 (Cont.) Date Format Models for the ROUND and TRUNC Date Functions

Format Model

Rounding or Truncating Unit

 

 

WW

Same day of the week as the first day of the year

 

 

IW

Same day of the week as the first day of the ISO year

 

 

W

Same day of the week as the first day of the month

 

 

DDD

Day

DD

 

J

 

 

 

DAY

Starting day of the week

DY

 

D

 

 

 

HH

Hour

HH12

 

HH24

 

 

 

MI

Minute

 

 

The starting day of the week used by the format models DAY, DY, and D is specified implicitly by the initialization parameter NLS_TERRITORY.

See Also: Oracle9i Database Reference and Oracle9i Database

Globalization Support Guide for information on this parameter

User-Defined Functions

You can write user-defined functions in PL/SQL or Java to provide functionality that is not available in SQL or SQL built-in functions. User-defined functions can appear in a SQL statement anywhere SQL functions can appear, that is, wherever an expression can occur.

For example, user-defined functions can be used in the following:

The select list of a SELECT statement

The condition of a WHERE clause

CONNECT BY, START WITH, ORDER BY, and GROUP BY clauses

The VALUES clause of an INSERT statement

The SET clause of an UPDATE statement

6-222 Oracle9i SQL Reference

User-Defined Functions

Note: Oracle SQL does not support calling of functions with boolean parameters or returns. Therefore, if your user-defined functions will be called from SQL statements, you must design them to return numbers (0 or 1) or character strings (’TRUE’ or ’FALSE’).

user_defined_function::=

package .

schema

.

function

user_defined_operator

DISTINCT

,

ALL

@

dblink

.

(

expr

)

The optional expression list must match attributes of the function, package, or operator.

Restriction on User-defined Functions The DISTINCT and ALL keywords are valid only with a user-defined aggregate function.

See Also:

CREATE FUNCTION on page 13-52 for information on creating functions, including restrictions on user-defined functions

Oracle9i Application Developer’s Guide - Fundamentals for a complete description on the creation and use of user functions

Prerequisites

User-defined functions must be created as top-level functions or declared with a package specification before they can be named within a SQL statement.

To use a user function in a SQL expression, you must own or have EXECUTE privilege on the user function. To query a view defined with a user function, you must have SELECT privileges on the view. No separate EXECUTE privileges are needed to select from the view.

Functions 6-223

User-Defined Functions

See Also:

CREATE FUNCTION on page 13-52 for information on creating top-level functions

CREATE PACKAGE on page 14-52 for information on specifying packaged functions

Name Precedence

Within a SQL statement, the names of database columns take precedence over the names of functions with no parameters. For example, if the Human Resources manager creates the following two objects in the hr schema:

CREATE TABLE new_emps (new_sal NUMBER, ...);

CREATE FUNCTION new_sal RETURN NUMBER IS BEGIN ... END;

then in the following two statements, the reference to new_sal refers to the column new_emps.new_sal:

SELECT new_sal FROM new_emps;

SELECT new_emps.new_sal FROM new_emps;

To access the function new_sal, you would enter:

SELECT hr.new_sal FROM new_emps;

Here are some sample calls to user functions that are allowed in SQL expressions:

circle_area (radius) payroll.tax_rate (empno)

hr.employees.tax_rate (dependent, empno)@remote

Example To call the tax_rate user function from schema hr, execute it against the ss_no and sal columns in tax_table, and place the results in the variable income_tax, specify the following:

SELECT hr.tax_rate (ss_no, sal)

INTO income_tax

FROM tax_table

WHERE ss_no = tax_id;

Naming Conventions

If only one of the optional schema or package names is given, then the first identifier can be either a schema name or a package name. For example, to

6-224 Oracle9i SQL Reference

User-Defined Functions

determine whether PAYROLL in the reference PAYROLL.TAX_RATE is a schema or package name, Oracle proceeds as follows:

1.Check for the PAYROLL package in the current schema.

2.If a PAYROLL package is not found, then look for a schema name PAYROLL that contains a top-level TAX_RATE function. If no such function is found, then return an error.

3.If the PAYROLL package is found in the current schema, then look for a TAX_ RATE function in the PAYROLL package. If no such function is found, then return an error.

You can also refer to a stored top-level function using any synonym that you have defined for it.

Functions 6-225

User-Defined Functions

6-226 Oracle9i SQL Reference

Соседние файлы в папке Oracle selected docs