I am getting error for the code below
I am trying to load log table with below columns
SP_MASTER_VIEW_TO_TABLES_RS
V_QUOTE_VIEWNAMES_TO_BE_LOADED_TO_TABLE
these are the variables defined within SP
var V_INSERT_DIM_MASTER_SP_LOG_RS = `INSERT INTO DB.SCHEMA.TABLENAME
SELECT ` + SP_MASTER_VIEW_TO_TABLES_RS + ` ,` + V_QUOTE_VIEWNAMES_TO_BE_LOADED_TO_TABLE + ` ,` + `getdate(), ''EXECUTED SUCCESSFULLY'';`
var V_EXEC_INSERT_DIM_MASTER_SP_LOG_RS = snowflake.createStatement( {sqlText: V_INSERT_DIM_MASTER_SP_LOG_RS} );
var V_RESULT_INSERT_DIM_MASTER_SP_LOG_RS = V_EXEC_INSERT_DIM_MASTER_SP_LOG_RS.execute();
V_RESULT_INSERT_DIM_MASTER_SP_LOG_RS.next();
Error
Execution error in store procedure SP_MASTER_SP: SQL compilation error: syntax error line 2 at position 72 unexpected '('. parse error line 28 at position 63 near '32'. parse error line 29 at position 138 near '44'. parse error line 52 at position 64 near '32'. parse error line 53 at position 138 near '44'. At Statement.execute, line 54 position 98
It's really hard to debug without seeing the real procedure. If you can't share the real procedure, please create a simplified version, run it and share the error belonging to the procedure.
I see that you directly add SP_MASTER_VIEW_TO_TABLES_RS and V_QUOTE_VIEWNAMES_TO_BE_LOADED_TO_TABLE variables to the SQL statement, so the value of them is important. Can't you share something like this?
create or replace procedure proc()
returns string
language javascript
as
$$
SP_MASTER_VIEW_TO_TABLES_RS = 'abc';
V_QUOTE_VIEWNAMES_TO_BE_LOADED_TO_TABLE = 'xyz';
var V_INSERT_DIM_MASTER_SP_LOG_RS = `INSERT INTO DB.SCHEMA.TABLENAME
SELECT ` + SP_MASTER_VIEW_TO_TABLES_RS + ` ,` + V_QUOTE_VIEWNAMES_TO_BE_LOADED_TO_TABLE + ` ,` + `getdate(), 'EXECUTED SUCCESSFULLY';`
var V_EXEC_INSERT_DIM_MASTER_SP_LOG_RS = snowflake.createStatement( {sqlText: V_INSERT_DIM_MASTER_SP_LOG_RS} );
var V_RESULT_INSERT_DIM_MASTER_SP_LOG_RS = V_EXEC_INSERT_DIM_MASTER_SP_LOG_RS.execute();
V_RESULT_INSERT_DIM_MASTER_SP_LOG_RS.next();
$$;
Although all these details are missing, I noticed that this part has an error:
`getdate(), ''EXECUTED SUCCESSFULLY'';`
You should not use double "single quotes", so it should be like this:
`getdate(), 'EXECUTED SUCCESSFULLY';`
Related
I set this up a while ago in something that is not really used. I was under the impression that it worked at the time, but I'm trying to test it now and am getting some errors.
Here is my snowflake code:
var rs = snowflake.createStatement( { sqlText: "select count(*) from toptal.stage_resellers" } ).execute();
rs.next();
var resellers = rs.getColumnValue(1);
I that that var resellers was going to define a variable that would have the number of rows in stage_resellers, but I'm not confident of that.
I decided to test it just by inserting the value into a logging table that I am using into an unused column for the time being.
Here is that code:
var stmt1 = snowflake.execute ( { sqlText:`insert into toptal.processing_executions values ('merge into dim_resellers', current_timestamp, 'processing', :resellers);`});
I'm getting this error:
Execution error in store procedure PROCESSING: SQL compilation error: error line 1 at position 110 Bind variable :resellers not set. At Snowflake.execute, line 47 position 24
I tried futzing around with setting the variable, to no avail.
I have a feeling that I am mixing up environments here, but I'm not sure what's going on.
To execute:
var stmt1 = snowflake.execute ( { sqlText:`insert into toptal.processing_executions values ('merge into dim_resellers', current_timestamp, 'processing', :resellers);`});
variables has to be provided as ? or :1. Code becomes:
var stmt1 = snowflake.execute({
sqlText: `insert into toptal.processing_executions
values ('merge into dim_resellers', current_timestamp,
'processing', ?)`
,binds: [resellers] } );
More at documentation: Binding Variables
It looks like Snowflake doesn't process parameter binding for SHOW USER statements in JS like this.
var sql_cmd = "SHOW USERS LIKE ?;";
var username = "user.name"
var stmt = snowflake.createStatement({sqlText: sql_cmd, binds:[username]});
var users = stmt.execute();
It just gives me an error saying that
SQL compilation error: syntax error line 1 at position 16 unexpected '?'. At Statement.execute, line 14 position 18
How do I make it work?
Is there a more accurate docs on what is supported by the binds feature? I feel like it should support all SQLs but looks like it doesn't work on CREATE either on another thread I found here.
Can you try this as an alternative? Note to use the SHOW USERS command you must execute the proc as a CALLER: https://docs.snowflake.com/en/sql-reference/stored-procedures-rights.html#caller-s-rights-stored-procedures
CREATE OR REPLACE PROCEDURE user_bind(username VARCHAR)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var sql_command = "SHOW USERS LIKE '" + USERNAME + "'";
var stmt = snowflake.createStatement( {sqlText: sql_command} );
var result1 = stmt.execute();
result1.next();
return result1.getColumnValue(1);
$$
;
alternatively you dont need a parameter and you could just use the variable within the SP
var username= 'USER1';
var sql_command = "SHOW USERS LIKE '" + username + "'";
call the stored proc
CALL user_bind ('USER1'); --or
CALL user_bind ();
I tried to get paths enclosed by double quotes (ex: "path"."to"."element"). It also strips any bracket-enclosed array element references (like "[0]")
var path_name = "regexp_replace(regexp_replace("customers[0].name",'\\[(.+)\\]'),'(\\w+)','"\\1"')" ;
I tried this method but it is displaying error
So this is a really poorly written question. But lets play the guessing game anyways.
So you have a Javascript stored procedure, and you have that line it side it, and it doesn't work as you expect: lets guess it looks like:
create or replace procedure sp()
returns VARCHAR
language javascript
as
$$
var txt = '"customers[0].name"';
var sql_regexp1 = '\\\\[(.+)\\\\]';
var sql_regexp2 = '(\\\\w+)';
var sql_rep_2 = '\"\\\\1\"';
var full_rep1 = "regexp_replace('" + txt + "','"+ sql_regexp1 +"')";
var full_rep2 = "select regexp_replace(" + full_rep1 + ",'"+ sql_regexp2 +"','"+ sql_rep_2 + "');";
//return full_rep2;
var statement = snowflake.createStatement( {sqlText: full_rep2} );
var result_set1 = statement.execute();
result_set1.next()
return result_set1.getColumnValue(1);
$$;
;
and if you uncomment out the early return to can see the full_rep2
thus you can test that the inner SQL
select regexp_replace('"customers[0].name"','\\[(.+)\\]');
gives:
REGEXP_REPLACE('"CUSTOMERS[0].NAME"','\[(.+)\]')
"customers.name"
lets assume that's correct!
then you can check the outer replace:
select regexp_replace(regexp_replace('"customers[0].name"','\\[(.+)\\]'),'(\\w+)','"\\1"');
which gives:
REGEXP_REPLACE(REGEXP_REPLACE('"CUSTOMERS[0].NAME"','\[(.+)\]'),'(\W+)','"\1"')
""customers"."name""
and if we call the stored procedure:
call sp();
we get:
SP
""customers"."name""
So this was "how I debugged the SQL/Javascript" to have "valid working SQL. The question then becomes, what output did you want. And can you get there from here.
I have a stored procedure
SP1 which has the signature SP1("TABLE_NAME")
And SP1 creates a table with the parameter passed
My second stored procedure has the signature SP2("PROCEDURE_NAME","PROCEDURE_PARAMETERS")
below is a snippet of the code inside SP2
v_sqlCode = `CALL ` + PROCEDURE_NAME + `(` + P_PROCEDURE_PARAMETERS + `)`;
try{
var sqlStmt = snowflake.createStatement({sqlText : v_sqlCode});
var sqlRS = sqlSt,t.execute();
}
Unfortunately I keep getting the error invalid identifier for the P_PROCEDURE_PARAMETER
So if I did
CALL SP2('SP1','C')
snowflake would say invalid identifier 'C' any ideas?
Snowflake would say invalid identifier 'C' any ideas?
The case is simple:
v_sqlCode = `CALL ` + PROCEDURE_NAME + `(` + P_PROCEDURE_PARAMETERS + `)`;
Call of: CALL SP2('SP1','C') becomes: CALL SP1(C) instead of CALL SP1('C'). Here C is an identifier and I guess you expect it to be a string literal.
Passing parameters as string is potentially dangerous as it is prone to SQL Injection.
Call using dollar-quoting:
CALL SP2('SP1',$$'C'$$)
I compiled a stored procedure but I'm unable to execute it - getting this error:
Execution error in stored procedure SAMPLE_ETL_MONITORING_PROC: Stored procedure execution error: Requested information on the current user is not accessible in stored procedure. At Statement.execute, line 18 position 45
I have write access to the database but not sure If I have to include any commands/statements in the script. Can anyone please suggest what needs to be done?
Here is the script:
CREATE OR REPLACE PROCEDURE sample_etl_monitoring_proc()
returns string not null
language javascript
as
$$
var insert_cmd = `
truncate table OOBE.monitoring.load_history_1
`
var sql_insert = snowflake.createStatement({sqlText: insert_cmd});
var insert_result = sql_insert.execute();
var stream_select_cmd = `
insert into OOBE.monitoring.load_history_1
select * from (
select * from OOBE.information_schema.load_history
union
select * from snowplow.information_schema.load_history);
`
var sql_select_stream = snowflake.createStatement({sqlText: stream_select_cmd});
var select_stream_result = sql_select_stream.execute();
return '👍';
$$;
I reproduced the error and got this to work by adding an explicit caller's right declaration:
CREATE OR REPLACE PROCEDURE sample_etl_monitoring_proc()
returns string not null
language javascript
execute as caller
as