java knowledge essays finishing - scalar and table-valued functions

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

Very simple function returns an integer value, then you can call the stored procedure, but called differently, as the value of the function call table above does not require the owner, as long as you can write the function name for the label valued function, it is necessary to add the owner, such owner is dbo.

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

 

Guess you like

Origin www.cnblogs.com/ashanxiaoya/p/11243931.html