Types of user defined functions:
A. Scalar valued functions
B. Table values functions
Scalar valued functions: These functions will return a scalar value to the calling environment
Syntax: CREATE FUNCTION < FUNCTION_NAME > (@PARA 1 DATA TYPE, @PARA 2 DATATYPE,….) RETURNS
AS
BEGIN
DECLARE @VARIABLE DATATYPE
…….
………….
RETURN @VARIABLE
END
Syntax to execute the user defined function:
SELECT/PRINT DBO.FUNCTIONNAME(VALUE1,VALUE2,…………)
Note: The number of values supplied through PRINT/SELECT statement must be equal to the number parameters.
Example: Write a function to find the product of two numbers
CREATE FUNCTION F1 (@A INT,@B INT)
RETURNS INT
AS
BEGIN
DECLARE @C INT
SET @C = @A * @B
RETURN @C
END
SELECT/PRINT DBO.F1 (3,5)

No comments:
Post a Comment