Helpers to avoid repeating some plumbing on Snowflake stored procedures
npm install snowjs-helperssnowsql-helpers: Extending Snowflake stored procedures with helpers
===========================================================
Motivation:
----------
Snowflake is a great platform. And using JS for stored procedures is nice I have no complains with that.
However sometimes the current API forces you to do a lot of repetitive work. For example when you are executing a
SQL statement or trying to retrieve results.
The Snowflake stored procedures do not allow you to do any kind of import. So this very simple script implements something like that.
In your stored procedure body you can do:
``javascript`
CREATE ....
$$
"@USING_
$$
The tool will map the snipped name to
So you can write your store procedures, then do
``
snowsql-helpers file.sql outdir
and the deploy the modified file.
I hope that helps :)
Installation
==================
npm install -g snowsql-helpers
Available snippets
==================
Currently there are only two snippets available:
EXEC Snippet
--------------
it allows you to execute queries like:
`javascriptSELECT CURRENT_DATE
EXEC();`
if you want to pass arguments you can use:
`javascriptSELECT Employee where EmpID = ?
EXEC(,[PARAM1]);`
To do something like a select into then you can do:
`javascriptSELECT EmployeeName, Salary where EmpID = ?
EXEC(,[PARAM1]);`
[vEmpName, vSalary] = INTO();
or as a one liner:
`javascriptSELECT Employee where EmpID = ?
[vEmpName, vSalary] = EXEC(,[PARAM1]);`
This helper has some other nice things like:
it sets a global variable for:
* ROW_COUNT
* ACTIVITY_COUNT
* MESSAGE_TEXT
* SQLCODE
* SQLSTATE
So you can easily do things like:
`javascript Delete from Employee where EmpID = ?
EXEC(,[PARAM1]);`
if (ACTIVITY_COUNT) {
return "employee was deleted";
}
else {
return "no employees were deleted";
}
or
`javascript Select EmployeeName from Employee where EmpID = ?
EXEC(,[PARAM1]);employee ${vEmpName} was found
if (ROW_COUNT) {
[vEmpName] = INTO();
return ;`
}
else {
return "no employees were deleted";
}
Cursor SNIPPET
-----------------
A lot of database provide some kind of cursor functionality.
You can for example do something like this:
`sql`
DECLARE CURSOR C1 AS SELECT * FROM EMPLOYEE;
You can also set parameter for the cursor:
`sql`
DECLARE CURSOR C1 AS SELECT * FROM EMPLOYEE where EmpId = :var1;
So this snippet allows that too:
`javascript
var C1=new Cursor("SELECT * FROM EMPLOYEE");
var C2=new Cursor("SELECT EmpName From Employee where EmpId = ?",()=>[EmpID]);
//...
EmpID = 100;
//...
C2.OPEN();
[EmpName] = C2.FETCH();
``