With sql server as an example:
1, table-valued function
User-defined table function returns the value table data type, the result table is a set of a single SELECT statement.
Sample Code CREATE FUNCTION Test_GetEmployeeSalary
(
@EmployeeID VARCHAR (20) - parameters
)
RETURNS TABLE - a return type of table
AS
the RETURN
(
SELECT * the FROM dbo.TEmployee
the WHERE EmployeeID = @EmployeeID - acquiring data in the table by a sql query
)
- function calls
SELECT the FROM Test_GetEmployeeSalary * ( '. 1')
---------------------
2, scalar-valued function
Sample code:
the CREATE dbo.Test_GetMax the FUNCTION
(
@a the INT, - a function of two parameters
@b the INT
)
the RETURNS the INT - return type of the INT
the AS
the BEGIN
the DECLARE @max the INT;
the IF (@a> @ = B)
the BEGIN
the SET @a = @max;
the END
the ELSE
the BEGIN
the SET @b = @max
the END
the rETURN @max; - @max finally return the return value of
the END
- calling function
SELECT dbo.Test_GetMax (2,3);
note:
(1) must use the two-part name of the function to call a function, that is, the owner of the Object name, such as dbo.Test_GetMax (2,3)
(2) before all incoming parameters must be added @
(3) Do not write leak and wrong keywords, such as, returns, return
(4) not later returns with a variable, but variable with the type of the return value
(5) in the begin / end statement block, using the return
Transfer: https: //blog.csdn.net/luming666/article/details/78532695