Accessing data from a Python stored procedure

You can access data from a stored procedure by using the Snowpark library APIs.

You can use the Session object that Snowflake creates for your stored procedure to access data by calling APIs in the Snowpark library. For example, you can create a DataFrame for a table or execute a SQL statement.

The context for the session (including the privileges, current database and schema, and so on) is determined by whether the stored procedure runs with caller’s rights or owner’s rights. For details, see Accessing and setting the session state.

See the Snowpark Developer Guide for more information.

Data access example

In the following example, a Python method copies a specified number of rows from one table to another table. The method takes the following arguments:

  • A Snowpark Session object

  • The name of the table to copy the rows from

  • The name of the table to save the rows to

  • The number of rows to copy

The method in this example returns a string. If you run this example in a Python worksheet, change the return type for the worksheet to a String

def run(session, from_table, to_table, count):

  session.table(from_table).limit(count).write.save_as_table(to_table)

  return "SUCCESS"
Copy