Variables, IF/ELSE and CASE
Snowflake Scripting lets a block remember values and choose which statements to execute.
Understand the idea
A variable is a named value kept while the block runs. It is not automatically a table column or a persistent setting. Declare its type when the intended contract matters.
| Concept | What it means |
|---|---|
| DECLARE / LET | Introduce a variable. |
| := | Assign a value within the block. |
| IF / ELSE | Choose a statement branch. |
| CASE | Choose among multiple conditions or values. |
A concrete example
Returns Large. Change amount to 40 and the ELSE branch returns Small.
Remember: Keep variable scope, control flow and transaction boundaries separate.
Explain it step by step · full theory
Understand the idea
A variable is a named value kept while the block runs. It is not automatically a table column or a persistent setting. Declare its type when the intended contract matters.
BEGIN and END surround scripting statements. BEGIN TRANSACTION has a different purpose: it starts a transaction. A scripting block does not automatically mean every statement is one atomic transaction.
Follow the example
Within SQL statements, colon notation binds a scripting variable value. Inside ordinary scripting expressions use its name. EXECUTE IMMEDIATE with dollar quoting submits the complete anonymous block to clients that would otherwise split its semicolons.
Words used in this lesson
- DECLARE / LET
- Introduce a variable.
- :=
- Assign a value within the block.
- IF / ELSE
- Choose a statement branch.
- CASE
- Choose among multiple conditions or values.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Variables, IF/ELSE and CASE
Start with the requirement
Snowflake Scripting lets a block remember values and choose which statements to execute.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Start with the requirement
Snowflake Scripting lets a block remember values and choose which statements to execute.
- Apply the rule
A variable is a named value kept while the block runs. It is not automatically a table column or a persistent setting. Declare its type when the intended contract matters.
- Explain the result
Keep variable scope, control flow and transaction boundaries separate.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Manual practice in an authorized Snowflake learning account. Run lesson 1 setup first and select L2_OBJECTS_LAB.M10_LAB and your authorized warehouse in each session. Use fresh object names; do not replace existing objects. Read whole scripting blocks before running them. The website does not execute commands.
Classify an amount
EXECUTE IMMEDIATE $$
DECLARE
amount NUMBER DEFAULT 120;
label VARCHAR;
BEGIN
IF (amount >= 100) THEN
label := 'Large';
ELSE
label := 'Small';
END IF;
RETURN label;
END;
$$;What to expect and why
Returns Large. Change amount to 40 and the ELSE branch returns Small.
Bind a variable into SQL and use CASE
EXECUTE IMMEDIATE $$
DECLARE
amount NUMBER DEFAULT 0;
label VARCHAR;
BEGIN
SELECT CASE WHEN :amount IS NULL THEN 'Unknown'
WHEN :amount = 0 THEN 'Zero' ELSE 'Known' END INTO :label;
RETURN label;
END;
$$;What to expect and why
Returns Zero. The colon binds amount and the SELECT result is assigned into label.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Change the CASE input to NULL. What is returned?
Compare your reasoning
Unknown: the missing-value branch runs before the zero comparison.