In Snowflake, SQL error handling block fails to execute

Implementing error handling in Snowflake using a Try Catch block has been my focus. I've enclosed SQL queries within JavaScript for this purpose. However, upon executing the query, I noticed that it skips the Try Catch block and directly executes the return statement without running any of the queries inside.

CREATE OR REPLACE PROCEDURE "SP_N_1Test"("STAGE_S3" VARCHAR(16777216), "STAGE_OUTPUT" VARCHAR(16777216))
RETURNS VARCHAR(16777216)
LANGUAGE Javascript
EXECUTE AS CALLER
AS 
$$

var cmd = `truncate table STAGE2A.T001_IRF_STUDENT_FORM_S3`;
var my_sql_command1 = snowflake.createStatement({sqlText: cmd});
var result = my_sql_command1.execute();

var cmd1 = `''COPY INTO STAGE2A.T001_IRF_STUDENT_FORM_S3 
FROM ( select
FN_TrimStr($1) as   State,
FN_TrimStr($2) as   AdminCode,    
from @stage2a.''||:STAGE_S3||'') 
pattern= ''''.*_IRF_.*\\\\.csv''''
file_format = (type=csv, skip_header=1 )''`;
var my_sql_command2 = snowflake.createStatement({sqlText: cmd1});
var result1 = my_sql_command2.execute();

var cmd2 = `Insert into STAGE2B.T011_IRF_STUDENT_FORM_V001 (
   STATE_FLAG,
    STATE_CD,
    )
SELECT
   STATE_FLAG,
    STATE_CD,
    from  STAGE2A.V001_IRF_STUDENT_FORM_T001`;
var my_sql_command3 = snowflake.createStatement({sqlText: cmd2});
var result2 = my_sql_command3.execute();

return;
$$;

In addition to this issue, I have also tried to include a message in each Try block to indicate success when it runs successfully. However, attempts with

Print, Println, sustem.print.out()
do not seem to work.

Answer №1

It appears there is some confusion regarding the execution of SQL statements within the procedure. To properly execute SQLs, the snowflake API needs to be utilized:

Upon review, it seems that backticks (`) were used incorrectly in the code snippet provided. The premature closing of backticks turned everything into a template string and caused the function to return immediately. It is essential to learn how to correctly utilize the snowflake API for executing SQL commands.

In terms of generating an output, it is not feasible to output data in the manner shown in the code. Instead, a variable can be defined to hold the output or logs can be inserted into a table.

CREATE OR REPLACE PROCEDURE sp_test("STAGE_S3" VARCHAR(16777216), "STAGE_OUTPUT" VARCHAR(16777216))
RETURNS VARCHAR(16777216)
LANGUAGE Javascript
EXECUTE AS CALLER
AS 
'
var result = "";

try{ `truncate table "STAGE2A"."T001_IRF_STUDENT_FORM_S3"`;
   result += "table truncated ";
}
catch (err){}
...
result += "Load process completed for IRF_STUDENT_FORM_S3";

}

catch(err){}

return result; 
';

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

I'm curious why the news content doesn't display when I enter a title like "asdasd", but it works when I change the title to a number

:) Alright, let's assume the title is SampleTitle news.php It's not functioning properly so I'll switch my title to another. $user = new User(); $news = $user->ShowNews(SampleTitle); echo '<pre>'; va ...

Generate a key pair using the cryto library and then use it with the json

There's a new method called generateKeyPair in node 10, and I am utilizing it in the following way: const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", { modulusLength: 4096, publicKeyEncoding: { type: "spki", format: "pem ...

What could be the reason for my jQuery script not functioning properly on my web form that was inherited from a MasterPage?

In the head of my master page, I have included: <script src="Script/jquery.min.js"></script> <link href="Stylesheet/Master.css" rel="stylesheet" /> My webform (Login.aspx) inherits from this master page, and in Login.aspx I have: <a ...

What is the best way to display a specific column in 50 different databases?

I have 50 different databases in mysql, each with a specific column named product_number. The tables within the databases have different names. Is there a way to select or display this column from all of them? I am aware that I can easily show the data in ...

Encountering problem with image file encoding while using res.download in Express.js

My node.js server with expressjs is set up for local development, where I store and retrieve various files through basic HTTP calls. Most of the time, everything works smoothly. However, on rare occasions, a small number of files return to the end-user sig ...

Generating Encrypted Passwords in PHP Login Page

Currently, I am facing an issue with creating a login page that connects to my server and verifies if the hashed (SHA256) database password matches the input password on the login page. Unfortunately, I have not been successful in making it work. In the e ...

Close session when browser/tab is exited

After extensive searching online, I have been unable to find a satisfactory solution for ending a session when a browser or tab is closed without requiring the user to log off. I have attempted numerous JavaScript codes that I came across, but none of the ...

Utilizing Multer for temporarily storing a file in memory before parsing and transferring it to the database

As a newcomer to programming, I am attempting to describe my issue concisely. I am utilizing multer to upload an XML file in conjunction with a Node Express server and React with .jsx. My intention is to parse the uploaded file data and post it to a Postgr ...

Preventing users from copying and pasting information from my form by implementing javascript restrictions

I'm looking for a solution to prevent users from copying and pasting in my form using JavaScript. I want to restrict the ability to paste or copy any content into the form. Any assistance would be greatly appreciated! ...

Node.js server experiencing delays handling multiple requests causing timeouts

As someone who is not very experienced with node, I appreciate your patience. I have a node.js server with 2 routes. Throughout the day, both routes receive requests simultaneously. Route 1 runs smoothly, while route 2 is a long-running process that invol ...

Working with the React JS Reducer, I am limited to only printing the returned payload and unable to perform any other actions

I've been struggling with this problem non-stop for several days now. Here's my reducer file: import {ADD_TASK, GET_TASKS, GET_TASKS_ERROR, GET_TASKS_SUCCESS} from "./TaskActions"; export const INITIAL_STATE = { tasks: [], isFetching: ...

ID could not be retrieved from the checkbox

I am facing an issue with my HTML checkboxes. The ids are generated from an angular ng-repeat, but when I try to collect the id for use, it always returns as undefined. $("input:checkbox.time-check-input").each(function () { var ruleUnformatted = ""; ...

Expanding Lists in Bootstrap 3: A Step-by-Step Guide

I have been experimenting with Bootstrap's Example accordion code, which can be accessed via this link: http://jsfiddle.net/qba2xgh6/18/ <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true"> <div class="panel ...

React and Express: Encounter a proxy error when trying to proxy the request for /total from localhost_errors

This question is a continuation of my previous inquiry on React.js: Axois Post stay on pending(but i am getting data) Below is the content of my package.json file { "name": "my-app", "version": "0.1.0", "private": true, "dependencies": { "ax ...

Steps to redirect to a webpage by clicking on an image without relying on anchor tags

Is it possible to redirect to a new webpage without using an anchor tag when someone clicks on an image? Below is my code for the image. <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_l ...

React drag and drop feature now comes with autoscroll functionality, making

I am encountering an issue with my nested list Items that are draggable and droppable. When I reach the bottom of the page, I want it to automatically scroll down. Take a look at the screenshot below: As shown in the image, there are more items at the bot ...

Rejuvenating your HTML content with AJAX over time

My HTML page contains links to charts that refresh every time the page is reloaded. A friend mentioned that AJAX can automatically refresh the chart at specified intervals without reloading the entire HTML page. I would appreciate any help with the HTML ...

Is it possible to export files from Flash to CreateJS via the command line?

Is there a method to automatically run the createjs toolkit for Flash tool from the command line? I have multiple components that I need to export in bulk. Is it possible to do this in a batch process? ...

Distinguishing between `notEmpty` and `exists` in express-validator: what sets them apart

I'm confused about the distinction between exists and notEmpty in express-validator as they seem to function identically. ...

Angular: display many components with a click event

I'm trying to avoid rendering a new component or navigating to a different route, that's not what I want to do. Using a single variable with *ngIf to control component rendering isn't feasible because I can't predict how many variables ...