snowflake.core.view.ViewCollection

class snowflake.core.view.ViewCollection(schema: SchemaResource)

Bases: SchemaObjectCollectionParent[ViewResource]

Represents the collection operations on the Snowflake View resource.

With this collection, you can create, iterate through, and search for views that you have access to in the current context.

Examples

Creating a view instance:

>>> views = root.databases["my_db"].schemas["my_schema"].views
>>> new_view = View(
...     name="my_view",
...     columns=[
...        ViewColumn(name="col1"), ViewColumn(name="col2"), ViewColumn(name="col3"),
...     ],
...     query="SELECT * FROM my_table",
...    )
>>> views.create(new_view)
Copy

Attributes

database
root

Methods

create(view: View, mode: CreateMode = CreateMode.error_if_exists, copy_grants: bool | None = False) ViewResource

Create a view in Snowflake.

Parameters:
  • view (View) – The View object, together with the View’s properties: name, columns, query; secure, kind, recursive, comment are optional

  • copy_grants (bool, optional) – Whether to enable copy grants when creating the object. Default is False.

  • mode (CreateMode, optional) –

    One of the following enum values.

    CreateMode.error_if_exists: Throw an snowflake.core.exceptions.ConflictError if the view already exists in Snowflake. Equivalent to SQL create view <name> ....

    CreateMode.or_replace: Replace if the view already exists in Snowflake. Equivalent to SQL create or replace view <name> ....

    CreateMode.if_not_exists: Do nothing if the view already exists in Snowflake. Equivalent to SQL create view <name> if not exists...

    Default is CreateMode.error_if_exists.

Examples

Creating a view, replacing any existing view with the same name:

>>> views = root.databases["my_db"].schemas["my_schema"].views
>>> new_view = View(
...     name="my_view",
...     columns=[
...        ViewColumn(name="col1"), ViewColumn(name="col2"), ViewColumn(name="col3"),
...     ],
...     query="SELECT * FROM my_table",
...    )
>>> views.create(new_view, mode=CreateMode.or_replace)
Copy
items() ItemsView[str, T]
iter(*, like: Annotated[str, Strict(strict=True)] | None = None, starts_with: Annotated[str, Strict(strict=True)] | None = None, show_limit: Annotated[int, FieldInfo(annotation=NoneType, required=True, metadata=[Strict(strict=True), Ge(ge=1), Le(le=10000)])] | None = None, from_name: Annotated[str, Strict(strict=True)] | None = None, deep: Annotated[bool, Strict(strict=True)] | None = None) Iterator[View]

Iterate through View objects from Snowflake, filtering on any optional ‘like’ pattern.

Parameters:
  • like (str, optional) – A case-insensitive string functioning as a filter, with support for SQL wildcard characters (% and _).

  • starts_with (str, optional) – String used to filter the command output based on the string of characters that appear at the beginning of the object name. Uses case-sensitive pattern matching.

  • show_limit (int, optional) – Limit of the maximum number of rows returned by iter(). The default is None, which behaves equivalently to show_limit=10000. This value must be between 1 and 10000.

  • from_name (str, optional) – Fetch rows only following the first row whose object name matches the specified string. This is case-sensitive and does not have to be the full name.

  • deep (bool, optional) – Optionally includes dependency information of the view. Default is None, which is equivalent to False.

Examples

Showing all views that you have access to see:

>>> views = view_collection.iter()
Copy

Showing information of the exact view you want to see:

>>> views = view_collection.iter(like="your-view-name")
Copy

Showing views starting with ‘your-view-name-‘:

>>> views = view_collection.iter(like="your-view-name-%")
Copy

Using a for loop to retrieve information from iterator:

>>> for view in views:
...     print(view.name, view.query)
Copy
keys() KeysView[str]
values() ValuesView[T]