snowflake.snowpark.WhenMatchedClause.update¶
- WhenMatchedClause.update(assignments: Dict[str, Union[Column, None, bool, int, float, str, bytearray, Decimal, date, datetime, time, bytes, NaTType, float64, list, tuple, dict]]) WhenMatchedClause [source] (https://github.com/snowflakedb/snowpark-python/blob/v1.16.0/src/snowflake/snowpark/table.py#L79-L122)¶
Defines an update action for the matched clause and returns an updated
WhenMatchedClause
with the new update action added.- Parameters:
assignments – A list of values or a
dict
that associates the names of columns with the values that should be updated. The value ofassignments
can either be a literal value or aColumn
object.
Example:
>>> # Adds a matched clause where a row in source is matched >>> # if its key is equal to the key of any row in target. >>> # For all such rows, update its value to the value of the >>> # corresponding row in source. >>> from snowflake.snowpark.functions import when_matched, lit >>> target_df = session.create_dataframe([(10, "old"), (10, "too_old"), (11, "old")], schema=["key", "value"]) >>> target_df.write.save_as_table("my_table", mode="overwrite", table_type="temporary") >>> target = session.table("my_table") >>> source = session.create_dataframe([(10, "new")], schema=["key", "value"]) >>> target.merge(source, (target["key"] == source["key"]) & (target["value"] == lit("too_old")), [when_matched().update({"value": source["value"]})]) MergeResult(rows_inserted=0, rows_updated=1, rows_deleted=0) >>> target.sort("key", "value").collect() # the value in the table is updated [Row(KEY=10, VALUE='new'), Row(KEY=10, VALUE='old'), Row(KEY=11, VALUE='old')]
Note
An exception will be raised if this method or
WhenMatchedClause.delete()
is called more than once on the sameWhenMatchedClause
object.