'Raising exception in others on Snowflake

I'm migrating some stored procedures from Oracle to Snowflake and I need to raise an exception in others segment of exception on snowflake exception including a column from cursor. Here is an example from oracle:

create or replace PROCEDURE        sp 
AS
CURSOR SCGR
  IS
    SELECT SP.REQUEST_NUM,
…
    from table;
BEGIN
  FOR I IN SCGR
  LOOP
    BEGIN      
          INSERT
          INTO table(…)
  VALUES
            (…);
        EXCEPTION
        WHEN OTHERS THEN
          RAISE_APPLICATION_ERROR(-20001,'An error was encountered in CUST_GROUP_REQUEST insert for - '||I.REQUEST_NUM||' - '||SQLCODE||' -ERROR- '||SQLERRM);
          EXIT;
        END;
      
END;

With this part I'm having troubles in Snowflake:

EXCEPTION
            WHEN OTHERS THEN
              RAISE_APPLICATION_ERROR(-20001,'An error was encountered in CUST_GROUP_REQUEST insert for - '||I.REQUEST_NUM||' - '||SQLCODE||' -ERROR- '||SQLERRM);
              EXIT;

Can someone help me?



Solution 1:[1]

If you check the RAISE command, you will see that it accepts an exception name, not a string expression which you can build dynamically.

https://docs.snowflake.com/en/sql-reference/snowflake-scripting/raise.html

Do you really need to raise an exception, or want to return a result cotaining the exception details?

https://docs.snowflake.com/en/developer-guide/snowflake-scripting/exceptions.html?_ga=2.186509665.956198205.1648142906-1409600040.1607023304#handling-an-exception

return object_construct('Error type', 'Other error',
                        'SQLCODE', sqlcode,
                        'SQLERRM', sqlerrm,
                        'SQLSTATE', sqlstate);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gokhan Atil