Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use transaction in Snowflake?

How to use transaction with commit,rollback and try catch block in snowflake like sql server?

SQL Server:

BEGIN TRY
    BEGIN TRANSACTION 
        --Insert statement 1
        --Insert statement 2
    COMMIT
END TRY
BEGIN CATCH

    IF @@TRANCOUNT > 0
        ROLLBACK
END CATCH

As per the snowflake documentation we can use transaction like this.

BEGIN TRANSACTION NAME T1;
-- Insert statement 1;
-- Insert statement 2;
COMMIT;
like image 693
Musakkhir Sayyed Avatar asked Nov 27 '25 00:11

Musakkhir Sayyed


2 Answers

The TRANSACTION_ABORT_ON_ERROR session parameter allows statements that return an error to abort a non-autocommit transaction. All statements issued inside that transaction fail until a commit or rollback statement is executed to close the transaction.

https://docs.snowflake.net/manuals/sql-reference/parameters.html#transaction-abort-on-error

like image 77
Mark Avatar answered Nov 29 '25 23:11

Mark


As of today Snowflake does not provide an equivalent construct. We're working on a feature (to be released very soon) allowing aborting a transaction if any statement in it fails, which should address this use case.

like image 41
Marcin Zukowski Avatar answered Nov 30 '25 00:11

Marcin Zukowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!