Summary of methods for udfs in sqlserver

Source: Internet
Author: User

"User-defined functions" are common statements, while "user-defined functions" are written in SQL Server.

SQL Server 2000 allows users to create user-defined functions. User-defined functions can return values.

User-Defined Functions are classified into scalar functions or table-valued functions.

If the returns clause specifies a scalar data type, the function is a scalar value function. You can use multiple Transact-SQL statements to define scalar value functions.
If the returns clause specifies a table, the function is a table value function.
Table valued functions can also be divided into: Embedded table valued functions (intra-row functions) or multi-statement Functions

If the table specified by the returns clause does not contain a list of columns, this function is an embedded Table value function.
If the table type specified by the returns clause contains columns and their data types, this function is a multi-statement Table value function.
Scalar function example
CopyCodeThe Code is as follows: Create Function DBO. Foo ()
Returns int
As
Begin
Declare @ n int
Select @ n = 3
Return @ n
End

Nested table value function example Copy codeThe Code is as follows: Create Function DBO. Foo ()
Returns table
As
Return select ID, title from msgs

The nested table value function has only one SELECT statement.

Multi-statement Table value function example (Part) Copy codeThe Code is as follows: Create Function fn_findreports (@ inempid nchar (5 ))
Returns @ retfindreports table (empid nchar (5) primary key,
Empname nvarchar (50) not null,
Mgrid nchar (5 ),
Title nvarchar (30 ))
...

Pay attention to its returns section.

The following statements can be used in the body of a Multi-statement function. Statements not listed in the following list cannot be used in the function body.

Value assignment statement.
Control Flow statement.
Declare statement, which defines the local data variables and cursors of the function.
Select statement, which contains a selection list with an expression. The expression in the list grants the value to the local variable of the function.
A cursor operation that references a local cursor declared, opened, closed, and released in a function. Only fetch statements that assign values to local variables using the into clause are allowed. Fetch statements that return data to the client are not allowed.
Insert, update, and delete statements. These statements modify the local table variables of the function.
Execute statements call extended stored procedures.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.