getting error with below if condition in Snowflake procedure
if (col1 in ('a','b')) { //some condition };
is there any way that we can construct if condition?
Thanks in advance.
Assuming you are talking about JavaScript procedures, here is a list of JavaScript comparison and logical operators: https://www.w3schools.com/js/js_comparisons.asp
You could use something like col1 == 'a' or col1 == 'b' as a condition.
Snowflake procedure statement very different.
if you use javascript try
if (col1 in ['a','b']) { //some condition };
you can see more doc here
and like here
Related
SPLIT_PART function used in a select statement in Snowflake Stored procedure returns null value.
var stmt5 = snowflake.CreateStatement({sqltext:`Select SPLIT_PART(SPLIT_PART(:1,''/'',2),''.gz'',1)`,binds:[stagename]});
var queryText=stmt5.getSqlText();
var x=stmt5.execute();
x.next();
stagename is retrieved from the List #my_stage command result and the result of the Split Part is used in the Copy Command for inserting records into a snowflake table.
If somebody responds will share the code thru an email to help me fix the issue. Thanks in advance.
You're using JavaScript with backticks, so there's no need to escape the single quotes. By doing so, it actually looks like an empty string. This is how it would look in your SQL history tab:
Select SPLIT_PART(SPLIT_PART(:1,''/'',2),''.gz'',1)
If you put a file name in and try that:
Select SPLIT_PART(SPLIT_PART('mypath/myfile.gz',''/'',2),''.gz'',1)
This doesn't actually return a null value. It's a syntax error, so maybe this execute call is in a try/catch block. That would return a null value when you try to use .next(),
Try it with the quotes single:
var stmt5 = snowflake.CreateStatement({sqltext:`Select SPLIT_PART(SPLIT_PART(:1,'/',2),'.gz',1)`,binds:[stagename]});
Here is my sample code below:
CREATE OR REPLACE PROCEDURE database.schema.sp_sample(dynamic_columns VARCHAR, dynamic_where_clause VARCHAR)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
rs="Success";
try {
retrieve_queries_sql = `SELECT COL_1, COL_2, ? FROM table ? GROUP BY ?`;
var stmt = snowflake.createStatement( {sqlText: retrieve_queries_sql, binds:[DYNAMIC_COLUMNS, DYNAMIC_WHERE_CLAUSE, DYNAMIC_COLUMNS]} );
var rs = stmt.execute();
}
catch(err) {
rs= "Failed Message: "+err.message;
}
return rs;
$$;
When I try to bind these columns Snowflake throws an error Unexpected ? and I even tried using :1, :2 even that didnt work same error Unexpected :
Dont want to prefer interpolation ${dynamic_columns} or ${dynamic_where_clause} as it can lead to sql injection concerns
How to bind the arguments in the best possible way? Any suggestions appreciated.
in other UDF the parameters to the JavaScript function need to be ALL_CAPS in the JavaScript body, as the SQL part (the function declaration) the SQL is case insensitive, while java it's "how it really is" which is upper case. Or you can double quote then in the declaration, as lower case, to have then lower case in the body.
I am trying to create an UDTF where I am planning to pass the dynamic where clause as an argument to the UDTF
CREATE OR REPLACE FUNCTION FUNCTION_NAME(where_clause VARCHAR)
RETURNS TABLE ()
LANGUAGE JAVASCRIPT
AS
$$ var sql_command=SELECT COL1, COL2 FROM TABLE_1 JOIN TABLE_2 ON ....
+where_clause+ GROUP BY ...;
var stmt = snowflake.createStatement( {sqlText: sql_command} );
var resultSet = stmt.execute(); resultSet.next();
$$
Is this something possible and also I want to be able to handle quotes in the arguments during calling of the function
for example if the call is:
SELECT * FROM TABLE(FUNCTION_NAME('WHERE COL_1='value''));
How do I handle quotes during calling of the function. Thanks for the help in advance
I wanted to underline the answer of Felipe. JavaScript UDTFs do not support "Stored Procedures API".
https://docs.snowflake.com/en/sql-reference/stored-procedures-api.html
So you can not create and run a dynamic SQL inside of a JavaScript UDTF. It must have "processRow" callback function to process the values coming as parameters and return them as rows.
https://docs.snowflake.com/en/sql-reference/udf-js-table-functions.html#basic-hello-world-examples
You can create a stored procedure to run a dynamic SQL and return the result as an array/json, but it won't be effective.
For your original question, you just need to escape the quote by repeating it or using slash:
call FUNCTION_NAME('WHERE i=''1''');
call FUNCTION_NAME('WHERE i=\'1\'');
I am trying to get a Stored Procedure to use SQL date instead of Java date.
But I want to do this without modifying the SP.
This is what I would like to do.
<select id="updateThing" statementType="CALLABLE">
{
DECLARE #tmp DATETIME
SET #tmp = GETDATE()
CALL dbo.thingUpdate (
#{..., javaType=java.lang.Integer, jdbcType = NUMERIC},
...,
<choose>
<when test="datePassed != null">
#tmp,
</when>
<otherwise>
NULL,
</otherwise>
</choose>
...
)
}
</select>
I have tried moving the bracers around and I have tried doing the GETDATE() in place and using TIMESTAMP, but all of them seem to trigger an exception.
In the case of the above version the error is:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'
Any advice would be appreciated.
You can create another stored procedure that acts as a wrapper for the existing one. It should get the date if needed and invoke the original stored procedure.
I want to use case in sql statement where clause but I have a problem as I want to create a where clause condition on the basis of some value and I want to set a not in clause values on the basis of it
here is the query where am facing an issue
WHERE CODE = 'x' and
ID not in (
case
when 'app'='A' then '570','592'
when 'Q' then ID 592,90
else 592,90
END
but its not syntax
You could embed it into the SQL where clauses like this:
WHERE CODE='x' and (('app' = 'A' AND ID not in ('570', '592')) OR
('app' = 'Q' AND ID not in ('592','90')) OR ('app' != 'A' AND 'app' != 'Q' AND ID not in ('592','90'))
Or something like that. Creepy code though, so I'd suggest using different queries for different types of 'app' parameter or create a stored procedure to handle your needs.
Consider that you may be trying to combine what should actually be separate statements.
You may want to consider using a test condition (perhaps via a simple IF statement) to determine which specific T-SQL statement to actually execute.
Pseudo Code:
IF (/*Conditions are True*/)
BEGIN
--SQL Statement
END
ELSE IF (/*Some other conditions are True)
BEGIN
--SQL Statement
END
ELSE
BEGIN
--Failsafe SQL statement
END
The logical intent of the code produced will be much easier to understand and maintain going forward.
As 592 is always part of the set, and the "Q" case is the same as the default, just do like this:
where CODE = 'x' and ID not in (592, case app when 'A' then 570 else 90 end)