EXECUTE NOTEBOOK

Executes the notebook outside the Notebook Editor. For example, you can run EXECUTE NOTEBOOK by itself from a worksheet, nest it within another Snowflake executable such as a stored procedure or task, or use it in a third-party orchestrator.

The command runs the latest code from all cells in the notebook. Results are accessible from the Notebook Editor.

Note

EXECUTE NOTEBOOK also requires the QUERY_WAREHOUSE parameter to be set, otherwise an error occurs. To set the QUERY_WAREHOUSE parameter, use the ALTER NOTEBOOK command.

Syntax

EXECUTE NOTEBOOK <name>([ <parameter_string> [ , ... ] ]);
Copy

Required parameters

name

Specifies the identifier (i.e. name) for the notebook; must be unique for the schema in which the notebook is created. Must be fully qualified if the notebook is not stored in the current database.schema you are operating in.

In addition, the identifier must start with an alphabetic character and cannot contain spaces or special characters unless the entire identifier string is enclosed in double quotes (for example, “My object”). Identifiers enclosed in double quotes are also case-sensitive.

For more information, see Identifier requirements.

Optional parameters

parameter_string

Optionally pass in arguments to a notebook. In a Python cell in the notebook, you can access these arguments by using the sys.argv variable.

Only strings are supported; other data types (such as integers or booleans) are interpreted as NULL.

Access control requirements

A role used to execute this operation must have the following privileges at a minimum:

Privilege

Object

Notes

OWNERSHIP

Notebook

OWNERSHIP is a special privilege on an object that is automatically granted to the role that created the object, but can also be transferred using the GRANT OWNERSHIP command to a different role by the owning role (or any role with the MANAGE GRANTS privilege).

For instructions on creating a custom role with a specified set of privileges, see Creating custom roles.

For general information about roles and privilege grants for performing SQL actions on securable objects, see Overview of Access Control.

Example

The following example triggers the default version of the specified notebook without passing in any arguments:

EXECUTE NOTEBOOK MY_DB.PUBLIC.MY_NOTEBOOK();
Copy

Pass parameters to a notebook

You can optionally pass in arguments when running a notebook. In Python cells, you can access these arguments by using the sys.argv variable, which is a built-in Python list that holds command-line arguments.

You can use arguments to customize notebook behavior; for example, you can pass in input values, specify a target environment, or adjusting execution logic based on these arguments.

Example

EXECUTE NOTEBOOK MY_DATABASE.PUBLIC.MY_NOTEBOOK(
  'parameter_string a,b,c,d',
  'target_database=PROD_DB'
);
Copy

In a Python cell in the notebook, you can access each argument as a string in the sys.argv list.

To learn how to access and use these arguments from a notebook (including how to parse lists or extract key-value pairs), see Develop and run code in Snowflake Notebooks.