雪花脚本中的 Try&catch 子句

Try&catch clause in snowflake scripting

我想知道在 Snowflake 中是否有等同于来自 javascript 的纯 sql 中的 try catch 块。 我想要一个程序来检查所有视图并对它们做一些事情,但是有些视图是无效的,我想跳过这些。 javascript 版本如下所示:

create or replace procedure test_views_js()
returns varchar
language javascript
as 

$$
var sql = "select table_name from INFORMATION_SCHEMA.views where table_schema='TEST'";
var stmt = snowflake.createStatement ({sqlText:sql});
var result_set = stmt.execute();
var cnt = 0;
while (result_set.next()){
try{
    var sql_text = "select count(*) from "+result_set.getColumnValue(1);
    var stmt2 =  snowflake.createStatement ({sqlText:sql_text});
    var r = stmt2.execute();
    r.next();
    cnt+=r.getColumnValue(1);
}catch (err){
continue
}

}
return cnt;
$$

我可以用 sql 获得相同的结果吗?

更新

当我尝试将异常放入循环中时出现语法错误。当它在不同的地方时,语法是有效的,但是 'break is outside of loop' 。也许有一些我看不到的明显错字?

create or replace procedure test_views() 
returns integer not null 
language sql 
as 
declare 
sel varchar; 
row_cnt integer default 0; 
res resultset default 
    (select table_name 
     from INFORMATION_SCHEMA.views 
     where table_schema='TEST') ; 
c1 cursor for res; 

begin 
    for row_variable in c1 do 
        row_cnt:= (select count(*) from view_test); 
        exception when statement_error then continue; 
    end for; 
    return row_cnt; 
end;

是 - 这里记录了 RAISE/EXCEPTION 构造:https://docs.snowflake.com/en/developer-guide/snowflake-scripting/exceptions.html

可以在循环内使用异常。而不是:

for row_variable in c1 do 
        row_cnt:= (select count(*) from view_test); 
        exception when statement_error then continue; 
end for; 

所有代码都应该用自己的 BEGIN EXCEPTION END 块包装。

create or replace procedure test_views() 
returns integer not null 
language sql 
as
declare 
sel varchar; 
row_cnt integer default 0; 
res resultset default 
    (select table_name 
     from INFORMATION_SCHEMA.views 
     where table_schema='TEST') ; 
c1 cursor for res; 

begin 
    for row_variable in c1 do 
        begin
            row_cnt:= (select count(*) from view_test); 
        exception when other then continue; 
        end;
    end for; 
    return row_cnt; 
end;