To Nha Notes | July 2, 2023, 4:22 p.m.
There are three different types of Streams supported in Snowflake
A Standard (i.e. delta) stream tracks all DML changes to the source object, including inserts, updates, and deletes (including table truncates). Supported on standard tables, directory tables and views.
The syntax to create Standard stream is as below
CREATE OR REPLACE STREAM my_stream ON TABLE my_table;
An Append-only stream tracks row inserts only. Update and delete operations (including table truncates) are not recorded. Supported on standard tables, directory tables and views.
The syntax to create Append-only streams similar to Standard streams except that the APPEND_ONLY parameter value needs to be set to TRUE as below
CREATE OR REPLACE STREAM my_stream ON TABLE my_table APPEND_ONLY = TRUE;
Supported for External tables only. An insert-only stream tracks row inserts only. They do not record delete operations that remove rows from an inserted set.
The syntax to create Insert-only stream is as below
CREATE OR REPLACE STREAM my_stream ON EXTERNAL TABLE my_table INSERT_ONLY = TRUE;
--INSERT
SELECT * FROM MY_STREAM
WHERE metadata$action = 'INSERT'
AND metadata$isupdate = 'FALSE';
--UPDATE
SELECT * FROM MY_STREAM
WHERE metadata$action = 'INSERT'
AND metadata$isupdate = 'TRUE';
--DELETE
SELECT * FROM MY_STREAM
WHERE metadata$action = 'DELETE'
AND metadata$isupdate = 'FALSE';
https://thinketl.com/change-data-capture-using-snowflake-streams/
https://docs.snowflake.com/en/user-guide/streams-examples
https://www.mssqltips.com/sqlservertip/6962/snowflake-streams-change-data-capture/