: . , .
:
;
;
;
, , Transact-SQL, .
MS SQL Server 3 :
, RETURNS. , . , BEGIN...END, Transact-SQL, . , text, ntext, image, cursor timestamp.
CREATE FUNCTION function_name
(
[ { @parameter_name [ AS ] parameter_data_type
[ = default ] }
[,...n ]
]
)
RETURNS return_data_type
[ WITH [ ENCRYPTION ] [ SCHEMABINDING ] [ EXECUTE AS Clause ]]
[ AS ]
BEGIN
function_body
RETURN scalar_expression
END
function _ name
.
@ parameter_name
. .
1 024 . , .
@. , . . , .
Parameter_data_type
. Transact-SQL timestamp, cursor table .
[ = default ]
. default, , .
, DEFAULT . DEFAULT , .
return_data_type
. Transact-SQL timestamp, cursor table .
ENCRYPTION
, Database Engine CREATE FUNCTION . , , .
SCHEMABINDING
, , . SCHEMABINDING , , . , , .
, , :
.
ALTER, SCHEMABINDING.
EXECUTE AS
, , .. Database Engine , . , .
, , . , , , .
EXECUTE AS { CALLER | SELF | OWNER | 'user_name' }.
Function_body
Transact-SQL, .
function_body Transact-SQL, .
scalar_expression
, .
:
. ProductCategoryID .
CREATE FUNCTION getMaxPrice (@ProductCategoryID int)
RETURNS int
AS
BEGIN
DECLARE @ret int;
SELECT @ret = MAX(Price)
FROM Product
WHERE ProductCategoryID = @ProductCategoryID
IF (@ret IS NULL)
SET @ret = 0;
RETURN @ret;
END;
--
SELECT ProductCategoryName, getMaxPrice (ProductCategoryID) AS MaxPrice
FROM ProductCategory
WHERE ProductCategoryID = 1;