Exploring the try and catch clause in Snowflake scripting

Is there an equivalent of try-catch blocks in pure SQL in Snowflake for handling errors?

I am looking to create a procedure that will check all views and perform some action on them, skipping the invalid ones. The following JavaScript version accomplishes this:

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;
$$

Can I achieve the same result with SQL?

UPDATE

When attempting to include an exception within the loop, a syntax error is encountered. Placing it elsewhere results in 'break is outside of loop' error. Are there any hidden typos in the code that are causing this issue?

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;

Answer №1

If you want to incorporate exceptions inside a loop, consider the following approach:

for row_variable in c1 do 
        BEGIN 
            row_cnt:= (select count(*) from view_test); 
            EXCEPTION WHEN statement_error THEN CONTINUE; 
        END; 
end for; 

It is important to encase all the code within its own BEGIN EXCEPTION END block as shown below:

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;

Answer №2

Absolutely - you can find information on RAISE/EXCEPTION constructs in this documentation:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be the reason behind Express Validator's refusal to accept null values in optional fields?

I have set up express-validator to validate a form, with most fields being optional. The validation for my route looks like this: const ExpValidate = require('express-validator'); router.post('/api/posthandler', [ ExpValidate.body(&quo ...

What is the best way to distinguish between enabled buttons using Protractor?

I am facing a challenge with a table containing 20 buttons. Half of these buttons are disabled while the other half is enabled. I am looking for a way to filter out the enabled buttons and click on each of them using a for loop. The buttons that I want to ...

A guide on updating and monitoring the boolean state of individual elements within an array

I am attempting to create a feature in react native where pressing a TouchableHighlight changes its color. Currently, I have a state variable that toggles between true and false when the button is pressed. However, this causes all elements in the map to ...

Using Vue to implement a global editing function for all checkboxes and selects - dealing with object stickiness

Unique Ordering System Check out the Codepen here! Main Goal The main objective is to develop a dynamic ordering system that caters to customer needs. This involves uploading files, storing them as an array of objects, and generating a table with produ ...

Table 0 cannot be located

Having trouble with inserting data into the pay_cheque table, any assistance? private void btnadd_Click(object sender, EventArgs e) { string s = "insert into pay_cheque values('Sai','Sai','Sai','Sai','10.2 ...

Converting Persian calendar date to Gregorian in JavaScript

Looking for assistance in converting a date from 1379/10/02 to AD format, specifically to the date 2000/4/29 using the momentJs library. Any help with this task would be greatly appreciated. Thank you! ...

Troubleshooting: Issues with executing a PHP script from jQuery

I found the source code on this website: It's an amazing resource, but I'm facing an issue where my JavaScript doesn't seem to be executing the PHP script... When I set a breakpoint in Chrome's debugger before the penultimate line (}) ...

Challenge with Merging Bootstrap Clean Blog Template with React Application

I am facing difficulties while trying to merge the Bootstrap Clean Blog Template (Link Attached) into my React App. This template is available for free. After downloading the template, I created a new react project and moved all static assets & files ...

What could be the reason for this Javascript code not functioning as intended, failing to generate a random number between 1 and 3 when I click on any of the buttons

Could someone help me with generating a random number between 1 and 3 each time I click on one of the buttons (rock, paper, scissors)? I am new to programming and not sure what I'm doing wrong. <!doctype html> <html lang="en"> <head& ...

Is it possible to change XML using Ajax technology?

Is it possible to update a value in an XML file using JavaScript/Ajax? I've managed to access the XML file with Ajax and utilize its values in my script. Now, I want to send any updates made by the script back to the XML file on the server using Ajax ...

Unable to trigger onClick event in React class-based component

I came across the following code for a class-based component: class PostLike extends Component { constructor(props) { super(props); this.state = { likes: null, like_id: null } this.likeSubmit = t ...

The most effective method for transferring a JavaScript object between a TypeScript frontend and a Node.js backend

I need some advice on how to effectively share a JavaScript object between my Angular2 with Typescript frontend and NodeJS backend in an application I'm working on. Currently, I am using a .d.ts file for the frontend and adding a module.exports in the ...

Angular table elements can trigger a click event that redirects to a unique URL generated from the ID of the selected element

In the angular table, every element should be clickable. When any element in the table is clicked, it should use its ID to perform a new search and redirect to the URL based on that ID of the clicked element. Below is the JSON data from the initial URL. It ...

A step-by-step guide on how to smoothly hide an element using ng-hide in AngularJS

Currently, I am able to toggle an element's visibility based on a boolean condition in my controller. However, I am looking for a way to smoothly fade out the element if the condition is true instead of instantly hiding it. Any suggestions on how to a ...

Unusual Behavior Uncovered in jQuery Selectors

Have you ever noticed a peculiar behavior of jQuery selectors? I recently discovered that when the page contains elements with non-unique IDs, jQuery returns different results for the same selectors: Here's an example of HTML code: <button id=&ap ...

In Firefox, the Ajax Call is failing to initiate a response automatically

I am currently working on developing a dynamically generated accordion with nested subaccordions. A filter function has been successfully implemented to manipulate the content on the page using Ajax. In order to achieve this, the following function has bee ...

Exporting variables in Angular's Ahead of Time (AoT) compiler is

I recently attempted to incorporate dynamic configuration into my project, following a guide I found in this insightful post. While everything functions smoothly with the JiT compiler, I encountered the following error when attempting to build using the A ...

Stopping halfway through a jQuery toggle width animation to close again

Perhaps the question may be a bit unclear, but allow me to provide an example. When repeatedly clicking the button that toggles the width, the div continues to animate long after the button press, which is not visually appealing. My desired outcome is for ...

Is it possible to utilize a slot within a Vue.js loop?

I am encountering an issue with a template that is utilizing v-for to loop through. The template includes a named slot where the name is dynamically assigned within the loop. However, no content is displaying as expected. Can someone help me identify wha ...

If statement utilized for conditional looping

As I dive into the world of basic JavaScript, I'm eager to understand how to loop back to the beginning of a method under specific conditions. Consider this scenario: in order for the program to progress to the statement "The character you typed was, ...