id="returnstmt">RETURN statement

The RETURN statement returns from a routine. For SQL functions, it returns the result of the function. For an SQL procedure, it optionally returns an integer status value. For SQL table functions, it returns a table as the result of the function.

 

Syntax


Click to skip syntax diagram


Read syntax diagram

Skip visual syntax diagram>>-+--------+-RETURN--+------------------+--------------------->< '-label:-' +-expression-------+ +-NULL-------------+ '-query-expression-'

 

Description

label

Specifies the label for the RETURN statement. The label name cannot be the same as the routine name or another label within the same scope. For more information, see Labels.

expression

Specifies a value that is returned from the routine:

  • If the routine is a function, expression must be specified and the value and the value of expression must conform to the SQL assignment rules as described in Assignments and comparisons. If assigning to a string variable, storage assignment rules apply.

  • If the routine is a procedure, the data type of expression must be INTEGER. If the expression evaluates to the null value, a value of 0 is returned.

NULL

The null value is returned from the SQL function. NULL is not allowed in SQL procedures.

query-expression

Specifies a query-expression value that is returned from the routine. The query-expression is a common-table-expression or fullselect. A query-expression is only allowed in a table function.

 

Notes

Returning from a procedure:

RETURN restrictions:

 

Example

Use a RETURN statement to return from an SQL procedure with a status value of zero if successful, and –200 if not.

 BEGIN
   ...
   GOTO fail;
   ...
   success: RETURN 0
   failure: RETURN -200
...

END

Define a scalar function that returns the tangent of a value using the existing sine and cosine functions.

 CREATE FUNCTION mytan (x DOUBLE)
   RETURNS DOUBLE
   LANGUAGE SQL
   CONTAINS SQL
   NO EXTERNAL ACTION
   DETERMINISTIC
   RETURN SIN(x)/COS(x)                                   


[ Top of Page | Previous Page | Next Page | Contents |
Index ]