IF (Snowflake Scripting) | Snowflake Documentation (original) (raw)

ReferenceScripting referenceIF

An IF statement provides a way to execute a set of statements if a condition is met.

For more information on branching constructs, see Working with conditional logic.

Syntax

IF ( ) THEN ; [ ; ... ] [ ELSEIF ( ) THEN ; [ ; ... ] ] [ ELSE ; [ ; ... ] ] END IF;

Where:

_condition_

An expression that evaluates to a BOOLEAN.

_statement_

A statement can be any of the following:

Usage notes

Examples

Here is an example of a Snowflake Scripting IF statement inside a stored procedure:

CREATE OR REPLACE PROCEDURE example_if(flag INTEGER) RETURNS VARCHAR LANGUAGE SQL AS BEGINIF(FLAG=1)THENRETURN′one′;ELSEIF(FLAG=2)THENRETURN′two′;ELSERETURN′Unexpectedinput.′;ENDIF;END;BEGIN IF (FLAG = 1) THEN RETURN 'one'; ELSEIF (FLAG = 2) THEN RETURN 'two'; ELSE RETURN 'Unexpected input.'; END IF; END;BEGINIF(FLAG=1)THENRETURNone;ELSEIF(FLAG=2)THENRETURNtwo;ELSERETURNUnexpectedinput.;ENDIF;END; ;

Here is the command to call the stored procedure, along with the output:

+-------------------+

EXAMPLE_IF
Unexpected input.
+-------------------+

For more examples that use the IF statement, see: