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¶
- The keyword
THEN
is required. ELSEIF
is one word (no spaces).END IF
is two words.- After each
THEN
orELSE
clause, the body allows theBEGIN
andEND
keywords, but does not require them, even if the body contains more than one_statement_
. - If the
_condition_
is NULL, then it is treated as FALSE.
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)THENRETURN′one′;ELSEIF(FLAG=2)THENRETURN′two′;ELSERETURN′Unexpectedinput.′;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: