Unable to execute snowflake master procedure (calls another procedure) from ADF lookup - stored-procedures

I have a stored procedure in Snowflake which calls two other stored procedures. This is getting executed in Snowflake and working successfully. I want to call this using ADF. I have already tried calling other stored procedure in ADF pipeline and they work using lookup activity.
However, it is not working for this particular master procedure. I think ADF is not able to go to the child procedures.
Sample snowflake query:
CREATE OR REPLACE PROCEDURE CHILD1(DBNAME VARCHAR)
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var result="";
var sql_command = `Truncate table if exists ${DBNAME}.PUBLIC.EMPLOYEE`;
snowflake.execute ({sqlText: sql_command});
return result;
$$
;
CREATE OR REPLACE PROCEDURE MASTER(DBNAME VARCHAR)
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var result="";
var sql_command = `CALL CHILD1(?)`;
snowflake.execute ({sqlText: sql_command, binds: [DBNAME]});
return result;
$$
;
ADF design:
Lookup query:
#concat('CALL PUBLIC.EMPLOYEE(',pipeline().parameters.DB_NAME,')')
Passing the database name as parameter

Related

Is it possible to call procedure inside a view in snowflake?

i have below table and stored procedure.
D | state | temp
-------------------------------
2021-06-21 |New York |69
2021-06-22 |Texes |70
#stored procedure
create or replace procedure get_state_based_on_temp(temp_value integer)
returns table(state string)
language sql
as
$$
declare
res resultset default (select state from temp_table1 where temp <= :temp_value);
begin
return table(res);
end;
$$;
i am trying to create a view below
create or replace view tem_result_view as select doc.d as date_in, view_temp.state from temp_table1 doc, table(get_state_based_on_temp(69)) as view_temp;
But it reurns below error
Unknown table function GET_STATE_BASED_ON_TEMP
i am able to call the function as
call get_state_based_on_temp(70);
which return the result.
Any solution to this issue?
A procedure can not be called inside a view. UDFs / functions can be called inside a view.
Have you tried to use a SQL table function (UDTF)?
create or replace function get_state_based_on_temp(temp_value integer)
returns table (state varchar)
as
$$
select state from temp_table1 where temp <= temp_value
$$;
https://docs.snowflake.com/en/sql-reference/functions-table.html

Assigning default value to snowflake stored procedure arguments

Is it possible to have default values in arguments of Stored procedures of Snowflake. For the below example, I am getting error. Please help
syntax error line 1 at position 53 unexpected ''test''.
create or replace procedure test(arg1 string default 'test')
returns string not null
language sql
as
$$
begin
return arg1;
end;
$$
;
Snowflake's procedures applies polymorphism instead of using default value. This solution is when you do not want to call sp like func1(Null)
For example (sql scripting):
create or replace procedure func1(arg1 varchar, arg2 varchar)
...
create or replace procedure func1(arg1 varchar)
...
call func1(arg1 , 'some-default-value')
...
One option could be providing NULL as value and handle it at the begining of the stored procedure with COALSESCE:
create or replace procedure test(arg1 string)
returns string not null
language sql
as
$$
begin
arg1 := COALESCE(arg1, 'test');
return arg1;
end;
$$;
CALL test(NULL);
-- test
Setting a default value/values as arguments directly in Stored procedures is not available in Snowflake currently
The below link can be referred for the allowed syntax in Stored Procedures
https://docs.snowflake.com/en/sql-reference/sql/create-procedure.html#syntax

Is it possible execute remove stage using stored procedure?

I am trying to execute remove file from snowflake internal stage via stored procedure. I'm getting this error:
Execution error in stored procedure RM_STAGE: Stored procedure execution error: Unsupported statement type 'UNKNOWN'. At Statement.execute, line 5 position 21
Is it possible to execute remove command via stored procedure?
Many Thanks,
Sriga
It is possible. The procedure needs to be a caller’s rights stored procedure. Below is an example of a SP that removes all files from a stage.
create or replace procedure remove_stage_file()
returns float
language javascript
EXECUTE AS CALLER
as
$$
var my_sql_command = "remove #SPLITSTAGE" ;
var statement1 = snowflake.createStatement( {sqlText: my_sql_command} );
var result_set1 = statement1.execute();
return 0;
$$
;```

Unable to retrieve multiple rows using cursor from database table using stored procedure DB2

I am trying to retrieve multiple rows from table using stored procedure and it doesn't work for me. My stored procedure is as follows,
CREATE OR REPLACE PROCEDURE E_Enquiry
(IN SourceQueue1 VARCHAR(30) ) LANGUAGE SQL
BEGIN
DECLARE C1 CURSOR FOR
select CreationTime
from ms.Exception_Message
where SourceQueue = SourceQueue1;
open c1;
END;
I am trying to call the stored procedure from Mule Anypoint Studio using the Database connector and have seen the result as null when I print the payload with a logger. If I try to modify the query as it returns a single row it works (without using cursor).
Please help to resolve this. Thanks.
What platform and version of DB2?
Try the adding
DYNAMIC RESULT SETS 1
WITH RETURN TO CLIENT
Like so:
CREATE OR REPLACE PROCEDURE E_Enquiry
(IN SourceQueue1 VARCHAR(30) )
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE C1 CURSOR WITH RETURN TO CLIENT FOR
select CreationTime
from ms.Exception_Message
where SourceQueue = SourceQueue1;
open c1;
END;

Call one stored procedure to another stored procedure in DB2

Problem that I was facing is that I have to call one stored procedure from another stored procedure in DB2 database. Kind example that I am giving right below.
I have one stored procedure:
CREATE OR REPLACE PROCEDURE Proc1()
IS
Declare myName in varchar;
BEGIN
Select fname into myName from student where fname='x'; // is returning unique value
-- here call anoher proc2
END;
Now so this proc1 procedure is going to call this proc2 procedure.
Now I have second stored procedure:
CREATE OR REPLACE PROCEDURE Proc2(Name in varchar)
IS
BEGIN
-- do anything
END;
I solved this problem,
So solution is like If we want to execute proc using sql command then syntex is like below,
call Proc2('My Name');
We can use this same approach inside our proc also.
For that we have to follow some steps. Lets say that our above sql call is statement that we want to execute. we are going to convert that statement into String and pass necessary parameter by concating variable values. Then execute statement.
CREATE OR REPLACE PROCEDURE Proc1()
IS
Declare myName in varchar;
-- stmt variable is to execute our proc
STMT VARCHAR(4000);
BEGIN
Select fname into myName from student where fname='x'; // is returning unique value
-- this is our logic
STMT :='call Proc2('||myName||')';
EXECUTE IMMEDIATE STMT;
END;

Resources