0
0
Snowflakecloud~10 mins

Stored procedures in Python in Snowflake - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Stored procedures in Python
Define Python Stored Procedure
Deploy to Snowflake
Call Stored Procedure
Execute Python Code in Snowflake
Return Result to Caller
End
This flow shows how a Python stored procedure is defined, deployed, called, executed inside Snowflake, and returns a result.
Execution Sample
Snowflake
CREATE OR REPLACE PROCEDURE greet(name STRING)
  RETURNS STRING
  LANGUAGE PYTHON
  RUNTIME_VERSION = '3.8'
AS
$$
  def run():
    return f'Hello, {name}!'
$$;
This code creates a Python stored procedure in Snowflake that takes a name and returns a greeting string.
Process Table
StepActionInputExecution DetailOutput
1Define procedurename=STRINGProcedure code stored in SnowflakeProcedure created
2Call procedurename='Alice'Snowflake runs Python code with input 'Alice'Executes return statement
3Execute Python codename='Alice'return f'Hello, {name}!'Hello, Alice!
4Return resultHello, Alice!Result sent back to callerHello, Alice!
5End---
💡 Procedure execution ends after returning the greeting string.
Status Tracker
VariableStartAfter CallFinal
nameundefined'Alice''Alice'
return valueundefinedundefined'Hello, Alice!'
Key Moments - 3 Insights
Why do we specify LANGUAGE PYTHON and RUNTIME_VERSION in the procedure?
Because Snowflake needs to know the language and Python version to run the code correctly, as shown in execution_table step 1.
How does the input 'name' get passed into the Python code?
The input parameter 'name' is passed automatically by Snowflake when calling the procedure, as seen in step 2 and variable_tracker.
What happens if the Python code does not return a value?
The procedure would return NULL or cause an error; returning a value is required to send output back, as shown in step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 3?
A'Hello, Alice!'
B'name=Alice'
CProcedure created
DUndefined
💡 Hint
Check the Output column in execution_table row with Step 3.
At which step does Snowflake run the Python code?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the Action and Execution Detail columns in execution_table.
If the procedure is called with name='Bob', what changes in variable_tracker?
AThe return value becomes 'Hello, Alice!'
BNo change in variables
CThe 'name' variable changes to 'Bob' after call
D'name' becomes undefined
💡 Hint
Refer to variable_tracker rows for 'name' variable values.
Concept Snapshot
Stored procedures in Snowflake using Python:
- Define with CREATE PROCEDURE, specify LANGUAGE PYTHON and RUNTIME_VERSION
- Input parameters passed automatically
- Python code runs inside Snowflake
- Return value sends output back
- Call procedure with CALL proc_name(args)
Full Transcript
This visual execution shows how to create and run a Python stored procedure in Snowflake. First, you define the procedure with the Python code and specify the language and runtime version. Then, when you call the procedure with an input like a name, Snowflake runs the Python code inside its environment. The Python code uses the input and returns a result string. This result is sent back to the caller. Variables like the input parameter and return value change during execution as shown. Key points include specifying language and version, passing inputs, and returning outputs properly.