LET

Assigns an expression to a Snowflake Scripting variable, cursor, or RESULTSET.

For more information on variables, cursors, and RESULTSETs, see:

See also

DECLARE

Syntax

LET { <variable_assignment> | <cursor_assignment> | <resultset_assignment> }

The syntax for each type of assignment is described below in more detail.

Variable Assignment Syntax

Use the following syntax to assign an expression to a variable.

LET <variable_name> <type> { DEFAULT | := } <expression> ;

LET <variable_name> { DEFAULT | := } <expression> ;

Where:

variable_name

The name of the variable. The name must follow the naming rules for Object Identifiers.

type

A SQL data type.

DEFAULT expression or . := expression

Assigns the value of expression to the variable.

If both type and expression are specified, the expression must evaluate to a data type that matches.

For example:

EXECUTE IMMEDIATE $$
    DECLARE
        profit number(38, 2) DEFAULT 0.0;
    BEGIN
        LET cost number(38, 2) := 100.0;
        LET revenue number(38, 2) DEFAULT 110.0;

        profit := revenue - cost;
        RETURN profit;
    END;
$$
;

Cursor Assignment Syntax

Use one of the following syntaxes to assign an expression to a cursor.

LET <cursor_name> CURSOR FOR <query> ;
LET <cursor_name> CURSOR FOR <resultset_name> ;

Where:

cursor_name

The name to give the cursor. This can be any valid Snowflake identifier that is not already in use in this block. The identifier is used by other cursor-related commands, such as FETCH.

query

The query that defines the result set that the cursor iterates over.

This can be almost any valid SELECT statement.

resultset_name

The name of the RESULTSET for the cursor to operate on.

RESULTSET Assignment Syntax

Use the following syntax to assign an expression to a RESULTSET.

<resultset_name> := ( <query> ) ;

Where:

resultset_name

The name to give the RESULTSET.

The name should be unique within the current scope.

The name must follow the naming rules for Object Identifiers.

DEFAULT query or . := query

Assigns the value of query to the RESULTSET.

Back to top