I have a problem in Snowflake with Task that executes Stored Procedures and that SP is using a Session variable QUERY_TAG that I want to use for logging purposes. When the Task executes the SP, I'll get the error: "Session variable '$QUERY_TAG' does not exist" EXECUTE AS CALLER is there It doesn't matter where I try to set the QUERY_TAG (in the first Task precond-code or in the definition).
The Tasks and SP are created by me as SYSADMIN
When I'm executing the SP in a query editor (Snowflake, DBeaver etc) it runs fine, so no coding errors in the SP. SET QUERY_TAG = 'A nice query tag' CALL TASK_SCHEMA.SP_TASK_ONE()
This runs fine when I'm calling doing it in the Worksheet or DBeaver or similar. Both the ways in the SP works (inline SQL or by the getQueryTag function)
Here is the code for Tasks and SP
CREATE OR REPLACE TASK TASK_SCHEMA.TASK_ONE_PRECOND
WAREHOUSE = TASK_WH
SCHEDULE = '2 minute'
QUERY_TAG = 'My Query Tag'
AS
SET QUERY_TAG = 'My Query Tag 2'
CREATE OR REPLACE TASK TASK_SCHEMA.TASK_ONE
WAREHOUSE = TASK_WH
AFTER TASK_SCHEMA.TASK_ONE_PRECOND
AS
CALL TASK_SCHEMA.SP_TASK_ONE()
create or replace procedure TASK_SCHEMA.SP_TASK_ONE()
RETURNS VARCHAR(50)
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
as $$
function getQueryTag()
{
var QueryTag;
rs_QT = snowflake.execute ( { sqlText: `SELECT $QUERY_TAG;` } );
if( rs_QT.next())
{
QueryTag = rs_QT.getColumnValue(1); // get the QueryTag
}
return QueryTag;
}
var qtag = getQueryTag();
//rs = snowflake.execute ( { sqlText:
//`INSERT INTO "LOG"."TESTSESSIONLOG"
// ("SESSION_NAME")
//SELECT $QUERY_TAG
//` } );
snowflake.execute({
sqlText: `INSERT INTO LOG.TESTSESSIONLOG
(SESSION_NAME)
VALUES (?)`
,binds: [ qtag]
});
return "SESSION_OK";
$$;