Show TOC

RETURN StatementLocate this document in the navigation structure

Exits a function or procedure unconditionally, optionally providing a return value. Statements following RETURN are not executed.

Syntax
RETURN [ ( <expression> ) ]
Parameters

(back to top)

  • expression

    if supplied, the value of <expression> is returned as the value of the function or procedure.

    Within a function, the expression should be of the same data type as the RETURN data type of the function.

Examples

(back to top)

  • Example 1 returns the product of three numbers:
    CREATE FUNCTION product ( a numeric,
                    b numeric ,
                    c numeric)
    RETURNS numeric
    BEGIN
      RETURN ( a * b * c ) ;
    END
  • Example 2 calculates the product of three numbers:
    SELECT product (2, 3, 4)
    product (2,3,4)
    24
  • Example 3 avoids executing a complex query, if it is meaningless:
    CREATE PROCEDURE customer_products
    ( in customer_id integer DEFAULT NULL)
    RESULT ( id integer, quantity_ordered integer )
    BEGIN
      IF customer_id NOT IN (SELECT ID FROM Customers)
      OR customer_id IS NULL THEN
        RETURN
      ELSE
        SELECT ID,sum(
          SalesOrderItems.Quantity )
        FROM Products,
            SalesOrderItems,
            SalesOrders
        WHERE SalesOrders.CustomerID = customer_id
        AND SalesOrders.ID = SalesOrderItems.ID
        AND SalesOrderItems.ProductID = Products.D
        GROUP BY Products.ID
      END IF
    END
Usage

(back to top)

RETURN is used in procedures for Transact-SQL-compatibility, and is used to return an integer error code.

Standards

(back to top)

  • SQL—ISO/ANSI SQL compliant.
  • SAP Database products—Transact-SQL procedures use the return statement to return an integer error code.
Permissions