DataFrameWriter.saveAsTable(table_name: Union[str, Iterable[str]], *, mode: Optional[str] = None, column_order: str = 'index', create_temp_table: bool = False, table_type: Literal['', 'temp', 'temporary', 'transient'] = '', clustering_keys: Optional[Iterable[Union[Column, str]]] = None, statement_params: Optional[Dict[str, str]] = None, block: bool = True, comment: Optional[str] = None) Optional[AsyncJob][source]

Writes the data to the specified table in a Snowflake database.

  • table_name – A string or list of strings representing table name. If input is a string, it represents the table name; if input is of type iterable of strings, it represents the fully-qualified object identifier (database name, schema name, and table name).

  • mode

    One of the following values. When it’s None or not provided, the save mode set by mode() is used.

    ”append”: Append data of this DataFrame to the existing table. Creates a table if it does not exist.

    ”overwrite”: Overwrite the existing table by dropping old table.

    ”truncate”: Overwrite the existing table by truncating old table.

    ”errorifexists”: Throw an exception if the table already exists.

    ”ignore”: Ignore this operation if the table already exists.

  • column_order

    When mode is “append”, data will be inserted into the target table by matching column sequence or column name. Default is “index”. When mode is not “append”, the column_order makes no difference.

    ”index”: Data will be inserted into the target table by column sequence. “name”: Data will be inserted into the target table by matching column names. If the target table has more columns than the source DataFrame, use this one.

  • create_temp_table – (Deprecated) The to-be-created table will be temporary if this is set to True.

  • table_type – The table type of table to be created. The supported values are: temp, temporary, and transient. An empty string means to create a permanent table. Learn more about table types here.

  • clustering_keys – Specifies one or more columns or column expressions in the table as the clustering key. See Clustering Keys & Clustered Tables for more details.

  • comment – Adds a comment for the created table. See COMMENT. This argument is ignored if a table already exists and save mode is append or truncate.

  • statement_params – Dictionary of statement level parameters to be set while executing this action.

  • block – A bool value indicating whether this function will wait until the result is available. When it is False, this function executes the underlying queries of the dataframe asynchronously and returns an AsyncJob.


>>> df = session.create_dataframe([[1,2],[3,4]], schema=["a", "b"])
>>> df.write.mode("overwrite").save_as_table("my_table", table_type="temporary")
>>> session.table("my_table").collect()
[Row(A=1, B=2), Row(A=3, B=4)]
>>> df.write.save_as_table("my_table", mode="append", table_type="temporary")
>>> session.table("my_table").collect()
[Row(A=1, B=2), Row(A=3, B=4), Row(A=1, B=2), Row(A=3, B=4)]
>>> df.write.mode("overwrite").save_as_table("my_transient_table", table_type="transient")
>>> session.table("my_transient_table").collect()
[Row(A=1, B=2), Row(A=3, B=4)]