A RETURN statement causes an immediate exit from a block of SQL. If expression is supplied, the value of expression is returned as the value of the function or procedure. Subqueries can not be used in expression.
If the RETURN appears inside an inner BEGIN block, it is the outer BEGIN block that is terminated.
Statements following a RETURN statement are not executed.
Within a function, the expression should be of the same data type as the function's RETURNS data type.
Within a procedure, RETURN is used for Transact-SQL-compatibility, and is used to return an integer error code.
The following function returns the product of three numbers:
CREATE FUNCTION product (
c NUMERIC )
RETURN ( a * b * c );
Calculate the product of three numbers:
SELECT product(2, 3, 4);
product(2, 3, 4)
The following procedure uses the RETURN statement to avoid executing a complex query if it is meaningless:
CREATE PROCEDURE customer_products
( in customer_ID integer DEFAULT NULL)
RESULT ( ID integer, quantity_ordered integer )
IF customer_ID NOT IN (SELECT ID FROM Customers)
OR customer_ID IS NULL THEN
GROUP BY Products.ID