Translation reference to convert SQL Server BEGIN and COMMIT transaction to Snowflake SQL
Description
Snowflake SQL, a transaction can be started explicitly by executing a BEGIN statement. Snowflake supports the synonyms BEGIN WORK and BEGIN TRANSACTION. Snowflake recommends using BEGIN TRANSACTION.
A transaction can be ended explicitly by executing COMMIT. Read more about Snowflake Transactions here.
Sample Source Patterns
The following examples detail the BEGIN and COMMIT transaction statements.
SQL Server
IN -> SqlServer_01.sql
CREATE PROCEDURE TestTransactionASBEGINDROPTABLEIF EXISTS NEWTABLE; CREATE TABLE NEWTABLE(COL1 INT, COL2 VARCHAR); BEGIN TRANSACTION; INSERT INTO NEWTABLE VALUES (1, 'MICHAEL'); INSERT INTO NEWTABLE VALUES(2, 'JACKSON'); COMMIT TRANSACTION;END
IN -> SqlServer_02.sql
CREATE PROCEDURE TestTransactionASBEGINDROPTABLEIF EXISTS NEWTABLE; CREATE TABLE NEWTABLE(COL1 INT, COL2 VARCHAR); BEGIN TRANSACTION LabelA; INSERT INTO NEWTABLE VALUES (1, 'MICHAEL'); INSERT INTO NEWTABLE VALUES(2, 'JACKSON'); COMMIT TRANSACTION LabelA;END
Snowflake SQL
OUT -> SqlServer_01.sql
CREATE OR REPLACE PROCEDURE TestTransaction ()RETURNS VARCHARLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"transact"}}'EXECUTE AS CALLERAS$$ BEGINDROPTABLEIF EXISTS NEWTABLE; CREATE OR REPLACE TABLE NEWTABLE ( COL1 INT, COL2 VARCHAR ); BEGIN TRANSACTION; INSERT INTO NEWTABLE VALUES (1, 'MICHAEL'); INSERT INTO NEWTABLE VALUES(2, 'JACKSON'); COMMIT; END;$$;
OUT -> SqlServer_02.sql
CREATE OR REPLACE PROCEDURE TestTransaction ()RETURNS VARCHARLANGUAGE SQLCOMMENT ='{"origin":"sf_sc","name":"snowconvert","version":{"major":1, "minor":0},{"attributes":{"component":"transact"}}'EXECUTE AS CALLERAS$$ BEGINDROPTABLEIF EXISTS NEWTABLE; CREATE OR REPLACE TABLE NEWTABLE ( COL1 INT, COL2 VARCHAR ); BEGIN TRANSACTION !!!RESOLVE EWI!!! /*** SSC-EWI-0101 - COMMENTED OUT TRANSACTION LABEL NAME BECAUSE IS NOT APPLICABLE IN SNOWFLAKE ***/!!! LabelA; INSERT INTO NEWTABLE VALUES (1, 'MICHAEL'); INSERT INTO NEWTABLE VALUES(2, 'JACKSON'); COMMIT; END;$$;