User-defined function

User-defined function

A User-Defined Function, or UDF, is a function provided by the user of a program or environment, in a context where the usual assumption is that functions are built into the program or environment.

BASIC language

In some old implementations of the BASIC programming language, user defined functions are defined using the "DEF FN" syntax. More modern dialects of BASIC are influenced by the structured programming paradigm, where most or all code is written as user defined functions or procedures, and the concept becomes practically redundant.

Databases

In SQL databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in SQL statements. The standard distinguishes between scalar and table functions. A scalar function returns only a single value (or NULL), whereas a table function returns a (relational) table comprised of zero or more rows and each row with one or more columns.

User-defined functions in SQL are declared using the CREATE FUNCTION statement. For example, a function that converts Celsius to Fahrenheit might be declared like this:

CREATE FUNCTION dbo.CtoF(Celsius FLOAT) RETURNS FLOAT RETURN (Celsius * 1.8) + 32

Once created, a user-defined function may be used as expressions in SQL statements. For example, it can be invoked where most other intrinsic functions are allowed. This also includes SELECT statements, where the function can be used against data stored in tables in the database. Conceptually, the function is evaluated once per row in such usage. For example, assume a table named ELEMENTS, with a row for each known chemical element. The table has a column named BoilingPoint for the boiling point of that element, in Celsius. This query:

SELECT Name, CtoF(BoilingPoint) FROM Elements

would retrieve the name and the boiling point from each row. It invokes the CtoF user-defined function as declared above in order to convert the value in the column to a value in Fahrenheit.

Each user-defined function carries certain properties or characteristics. The SQL standard defines the following properties:
* language - defines the programming language in which the user-defined function is implemented; examples are SQL, C, or Java.
* parameter style - defines the conventions that are used to pass the function parameters and results between the implementation of the function and the database system (only applicable if language is not SQL).
* specific name - a name for the function that is unique within the database. Note that the function name does not have to be unique, considering overloaded functions.
* determinism - specifies whether the function is deterministic or not. The determinism characteristic has an influence on the query optimizer when compiling a SQL statement. The rewrite of SQL statements may not change the number of function invocations for non-deterministic functions.
* SQL-data access - tells the database management system whether the function contains no SQL statements (NO SQL), contains SQL statements but does not access any tables or views (CONTAINS SQL), reads data from tables or views (READS SQL DATA), or actually modifies data in the database (MODIFIES SQL DATA).

User-defined functions should not be confused with stored procedures. Stored procedures allow the user to group a set of SQL commands. A procedure can accept parameters and execute its SQL statements depending on those parameters. A procedure is not an expression and, thus, cannot be used like user-defined functions.

Some database management systems allow the creation of user defined functions in languages other than SQL. Microsoft SQL Server, for example, allows the user to use .NET languages for this purpose. DB2 and Oracle support user-defined functions written in C or Java programming languages.

QL Server 2000

There are three types of UDF in Microsoft SQL Server 2000:
#Scalar functions.
#Inline table-valued functions.
#Multistatement table-valued functions.

Scalar functions return a single data value (not a table) with RETURNS clause. Scalar functions can use all scalar data types, with exception of timestamp and user-defined data types. Inline table-valued functions return the result set of a single SELECT statement. Multistatement table-valued functions return a table, which was built with many TRANSACT-SQL statements.

User-defined functions can be invoked from a query like built-in functions such as OBJECT_ID, LEN, DATEDIFF, or can be executed through an EXECUTE statement like stored procedures.

Performance Notes:

1. On Microsoft SQL Server 2000 a table-valued function which 'wraps' a View may be much faster than the View itself. The following MyFunction is an example of a 'function-wrapper' which runs faster than the underlying view MyView:

CREATE FUNCTION MyFunction ()RETURNS @Tbl TABLE (StudentID VARCHAR(255), SAS_StudentInstancesID INT, Label VARCHAR(255), Value MONEY, CMN_PersonsID INT)AS

BEGIN

INSERT @Tbl(StudentID , SAS_StudentInstancesID , Label , Value , CMN_PersonsID )SELECT StudentID , SAS_StudentInstancesID , Label , Value , CMN_PersonsID FROM MyView -- where MyView selects (with joins) the same columns from large table(s)

RETURN

END

2. On Microsoft SQL Server 2005 the result of the same code execution is the opposite: view is executed faster than the 'function-wrapper'.

User-defined functions are subroutines made of one or more Transact-SQL statements that can be used to encapsulate code for reuse. It takes zero or more arguments and evaluates a return value. Has both control-flow and DML statements in its body similar to stored procedures.Does not allow changes to any Global Session State, like modifications to database or external resource, such as a file or a network.Does not support output parameter.DEFAULT keyword must be specified to pass the default value of parameter.Errors in UDF cause UDF to abort which, in turn, aborts the statement that invoked the UDF.CREATE FUNCTION CubicVolume-- Input dimensions in centimeters (@CubeLength decimal(4,1), @CubeWidth decimal(4,1) , @CubeHeight decimal(4,1) )RETURNS decimal(12,3)ASBEGIN RETURN ( @CubeLength * @CubeWidth * @CubeHeight )ENDData type supported in Microsoft® �SQL Server™ 2000Like a temporary table used to store results Mostly used to define temporary variable of type (table) and the return value of a UDFThe scope is limited to function, stored procedure, or batch in which it is definedAssignment operation is not allowed between (Table) variablesMay be used in SELECT, INSERT, UPDATE, and DELETE CREATE FUNCTION to create UDFALTER FUNCTION to change the characteristics of UDFDROP FUNCTION to remove UDF

External links

* [http://msdn2.microsoft.com/en-us/library/aa258261(SQL.80).aspx Microsoft SQL Server reference for CREATE FUNCTION]
* [http://dev.mysql.com/doc/refman/5.0/en/adding-functions.html MySQL manual section on UDFs]
* [http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0000917.htm DB2 CREATE FUNCTION statement]
* [http://www.petersap.nl/SybaseWiki/index.php/User_defined_SQL_functions Using user defined functions written in SQL in Sybase ASE]
* [http://www.petersap.nl/SybaseWiki/index.php/User_defined_Java_functions Using user defined functions written in Java in Sybase ASE]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • User Defined Function — Eine Benutzerdefinierte Funktion (engl. User Defined Function, Abkürzung UDF) in einer Programmierumgebung bezeichnet eine Funktion, die der Anwender selbst erstellen und in seine Projekte einbinden kann. Sie erfüllt eine Aufgabe wie ein Makro,… …   Deutsch Wikipedia

  • user-defined function — noun A program or subroutine written by an application programmer that returns the value of a mathematical function …   Wiktionary

  • Function point — A function point is a unit of measurement to express the amount of business functionality an information system provides to a user. Function points are the units of measure used by the IFPUG Functional Size Measurement Method. The IFPUG FSM… …   Wikipedia

  • Function key — A function key is a key on a computer or terminal keyboard which can be programmed so as to cause an operating system command interpreter or application program to perform certain actions. On some keyboards/computers, function keys may have… …   Wikipedia

  • User-generated content — UGC redirects here. For other uses, see UGC (disambiguation). User generated content (UGC) covers a range of media content available in a range of modern communications technologies. It entered mainstream usage during 2005 having arisen in web… …   Wikipedia

  • Main function (programming) — In some programming languages, the main function is where a program starts execution. It is generally the first user written function run when a program starts (some system specific software generally runs before the main function), though some… …   Wikipedia

  • Map (higher-order function) — In many programming languages, map is the name of a higher order function that applies a given function to each element of a list, returning a list of results. They are examples of both catamorphisms and anamorphisms. This is often called apply… …   Wikipedia

  • First-class function — In computer science, a programming language is said to support first class functions (or function literal) if it treats functions as first class objects. Specifically, this means that the language supports constructing new functions during the… …   Wikipedia

  • Strict function — A strict function in the denotational semantics of programming languages is a function f where . The entity , called bottom, denotes an expression which does not return a normal value, either because it loops endlessly or because it aborts due to …   Wikipedia

  • Help:User style — This Wikipedia help page is outdated. Please update this Wikipedia help page to reflect recent events or newly available information. Please see the talk page for more information. The user can customize fonts, colors, positions of links in the… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”