Implements error handling for Transact SQL. A group of Transact-SQL statements can be enclosed in a TRY block. If an error occurs in the TRY block, control is usually passed to another group of statements that is enclosed in a CATCH block.
Sample Source Patterns
The following example details the transformation for THROW statement without custom message.
[S0001][8134] Line 3: Divide by zero error encountered.
Snowflake SQL
CREATEORREPLACEPROCEDURE THROW_PROCEDURE ()RETURNSVARCHARLANGUAGESQLEXECUTEASCALLERAS$$BEGINBEGINSELECT TRUNC( 1/0); EXCEPTIONWHEN OTHER THEN LET DECLARED_EXCEPTION EXCEPTION; RAISE DECLARED_EXCEPTION;END;END;$$;
-20000 (P0001): Uncaught exception of type 'DECLARED_EXCEPTION' on line 9 at position 16
The following example details the transformation for THROW statement with a custom message.
SQL Server
CREATEPROCEDURE THROW_PROCEDURE ASBEGINTRYSELECT1/0;ENDTRYBEGINCATCHDECLARE @ERROR_MSG VARCHAR(max) ='DIVISION BY 0';THROW50000, @ERROR_MSG, 1;ENDCATCH
[S0001][50000] Line 7: DIVISION BY 0
Snowflake SQL
CREATEPROCEDURE THROW_PROCEDURE ASBEGINTRYSELECT1/0;ENDTRYBEGINCATCHDECLARE @ERROR_MSG VARCHAR(max) ='DIVISION BY 0';THROW50000, @ERROR_MSG, 1;ENDCATCH
ERROR NUMBER: 50000
MESSAGE: DIVISION BY 0
STATE: 1
For THROW statements without a custom message, the default message from SQL Server and Snowflake may be different.