Back to Trino

BEGIN

docs/src/main/sphinx/udf/sql/begin.md

4801.2 KB
Original Source

BEGIN

Synopsis

text
BEGIN
  [ DECLARE ... ]
  statements
END

Description

Marks the start and end of a block in a . BEGIN can be used wherever a statement can be used to group multiple statements together and to declare variables local to the block. A typical use case is as first statement within a . Blocks can also be nested.

After the BEGIN keyword, you can add variable declarations using statements, followed by one or more statements that define the main body of the SQL UDF, separated by ;. The following statements can be used:

  • Nested blocks

Examples

The following example computes the value 42:

sql
FUNCTION meaning_of_life()
  RETURNS integer
  BEGIN
    DECLARE a integer DEFAULT 6;
    DECLARE b integer DEFAULT 7;
    RETURN a * b;
  END

Further examples of varying complexity that cover usage of the BEGIN statement in combination with other statements are available in the .

See also