COMMIT en ROLLBACK in Snowflake SQL

Ever wondered what COMMIT and ROLLBACK do in Snowflake SQL?

I know I have.

Let me try to explain it using Snowflake SQL.

SQL
--Creating prerequisites.
CREATE DATABASE TEST;
CREATE TABLE T_D_PEOPLE (NAME VARCHAR);
INSERT INTO T_D_PEOPLE VALUES ('Bruce');

--Check out the table.
SELECT * FROM T_D_PEOPLE;

--AUTOCOMMIT is set to true. This means that statements are commited and cannot be rolled back. 
SHOW PARAMETERS LIKE '%COMMIT%' IN SESSION;

--Let's check if this is indeed true. 
INSERT INTO T_D_PEOPLE VALUES ('Clark');

--Both Bruce and Clark are in the table now.
SELECT * FROM T_D_PEOPLE;

--When I try to rollback, nothing happens: Clark is still in the table. 
ROLLBACK;

--Let's set AUTOCOMMIT to false
ALTER SESSION SET AUTOCOMMIT = false;

--Let's insert another name.
INSERT INTO T_D_PEOPLE VALUES ('Diana');

--Bruce, Clark and Diana are in the table now.
SELECT * FROM T_D_PEOPLE;

--However, when I rollback, Diana will dissappear, because the INSERT statement has not been committed yet.
ROLLBACK;

--If I do the same insert and commit right after however, the rollback will not roll the insert back because the INSERT statement has been committed.
INSERT INTO T_D_PEOPLE VALUES ('Diana');
COMMIT;

--Trying to rollback will not remove the inserted record because it has been committed. 
ROLLBACK;
SQL

Leave Comment