Execute stored procedure inside another stored procedure in snowflake - snowflake-cloud-data-platform

We created 2 stored procedures separately in snowflake. Now we have master stored procedure which we need to execute. This master stored procedure is written is way that the return value of first stored procedure would trigger second stored procedure.
When I am assigning return value of first stored procedure to variable , it seems like it is not getting assigned.
var CALL_CATALOG_SP_return_val= snowflake.execute({ sqlText: CALL_CATALOG_SP });
How do I ensure that I am second stored procedure runs only when I get particular value from first stored procedure.

Not sure, how you are doing it. I am able to do it as below
CREATE OR REPLACE procedure FIRST_PROC()
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$
A = 'DONE';
return A;
$$;
CREATE OR REPLACE procedure SECOND_PROC()
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
AS
$$
query = 'CALL FIRST_PROC()';
A= snowflake.execute({ sqlText:query });
A.next();
return_val = A.getColumnValue(1);
return return_val;
$$;
CALL SECOND_PROC();

Related

Why does setting session variable in Snowflake stored procedure not work?

When I set a session variable as caller in a Snowflake stored procedure, the procedure throws error "Error: Session variable '$T' does not exist (line 5)"
This is how I defined my procedure :
create or replace procedure test()
returns varchar()
language SQL
EXECUTE AS CALLER
as
$$
begin
set T = 0;
select $T;
return 'Done !';
end;
$$;
call test();
If I set the variable T before running the procedure it works, but if I don't I get the error
It's normal because when the script is compiled, there is no session variable. To be able to compile the snowflake script successfully, the session variable should exist.
Here is a workaround:
create or replace procedure test()
returns varchar()
language SQL
EXECUTE AS CALLER
as
$$
begin
set T = 0;
select getvariable('T');
return 'Done !';
end;
$$
;
call test();

write multipe statments in snowflake

Hi i have one doubt in snowflake how to write multiple update stments using stored procedure.
i have tried like below
create or replace procedure sp_multipleupdate()
returns table()
lANGUAGE sql
as
$$
declare res rsultset(
update TEST.PUBLIC.DEPT set Dname='PM' where deptid=10;
update TEST.PUBLIC.emp set name='veavi' where deptno=20;
update TEST.PUBLIC.loc set locname='del' where id=5;
)
begin
return table(res);
end;
$$;
getting error :
000006 (0A000): Multiple SQL statements in a single API call are not supported; use one API call per statement instead.
Syntax error: unexpected '('. (line 2)
please let me know how to write query to achive this task in snowflake server .
Multiple SQL statements inside the resultset are not supported.
Rather than writing the UPDATE statements like that I would create a more generic procedure and pass arguments to it, so maybe split the above one in 3 procedures since these UPDATE statements are for different tables.
Here is a sample of a generic stored procedure:
create or replace procedure find_invoice_by_id_via_execute_immediate(id varchar)
returns table (id integer, price number(12,2))
language sql
as
declare
select_statement varchar;
res resultset;
begin
select_statement := 'SELECT * FROM invoices WHERE id = ' || id;
res := (execute immediate :select_statement);
return table(res);
end;
You can read more here.

call a stored procedure from another stored procedure and bring values from first stored procedure to the second stored procedure

I am currently doing an assignment where I create a stored procedure "sp_master".
From that stored procedure, I read in the needed external data, internal data and then perform all the required logic to decided if the inputted information is valid.
I then need to call a stored procedure "sp_insert" from the "sp_master" stored procedure. From the "sp_insert" stored procedure I need to add a row to a table using the external data that was read into the "sp_master" stored procedure.
I know how to call a stored procedure from another stored procedure, but I am not sure how to bring the values across, which is what I need the help with
here is a snippet of my code to give an understanding
Create proc sp_master
#DeliveryID int, #FreightID int, #NoOfGoods int
as
--Perform reads and logic
begin
EXECUTE sp_Insert_delivery
end
then from sp_Insert
Create proc sp_exam_Insert_delivery
as
begin try
insert into dbo.Delivery
(FreightID, DeliveryID, NoOfGoods)
Values
(#FreightID, #DeliveryID, #NoOfGoods)
end try
--followed by catch
You just need to add the same parameters to sp_Insert_delivery and pass them in.
Create proc sp_master
#DeliveryID int, #FreightID int, #NoOfGoods int
as
--Perform reads and logic
begin
EXECUTE sp_Insert_delivery #DeliveryID, #FreightID, #NoOfGoods
end
.
Create proc sp_Insert_delivery
#DeliveryID int, #FreightID int, #NoOfGoods int
as
begin try
insert into dbo.Delivery
(FreightID, DeliveryID, NoOfGoods)
Values
(#FreightID, #DeliveryID, #NoOfGoods)
end try
--followed by catch

SQL Server stored procedure called from another stored proc returns command completed succesfully but no result set

I have two stored procedures; I am calling one stored proc from another.
I return a table from the 1st stored procedure. When I execute the 1st alone, I get the table correctly.
But when I call the 1st stored procedure from another stored procedure, it always returns command completed successfully and no results.
I call it like this in stored proc 2:
set #query = 'exec servername.dbo.storedproc1 #ClassName = ''' +
#ClassName +''', #StatusName = ''' + #StatusName
exec(#query)
Inside the outer procedure, create a temporary table with a similar schema to the result set returned from the inner procedure.
When calling the inner procedure, use insert..exec, like this:
insert #tempTable exec InnerProcedure
Then select the data from the temporary table.
First create a temporary table that captures data obtained from executing first procedure . Then use Select statement to retrieve desired data from temporary table. Don't forget to use drop the temporary table table after Select statement else next time you execute the procedure error will be thrown saying table already exist . On other hand you can use table variable to avoid dropping table as scope of table variable is limited to lifetime of the containing stored procedure.
`
Insert into #temptable
Exec sprcdre1
Select * from #temptable

Call SQLCLR procedure from another stored procedure and get 2 return values?

I am using a stored procedure that calls another SQLCLR stored procedure. The SQLCLR procedure returns 2 values.
How to get 2 return values from SQLCLR procedure to the T-SQL stored procedure?
I know normally from C# getting the return value using output parameter. But confusing how to get and hold return value in normal stored procedure.
SQLCLR Procedure:
public static void SendMailSP(out string Status, out string Message)
{
Status = "hi:";
Message = "Hello";
}
DB Procedure
CREATE PROCEDURE [dbo].udpTestOutParameter
{
DECLARE #Status varchar(100);
DECLARE #Message varchar(100);
EXECUTE dbo.SendMailSP #Status = #Status OUTPUT,#Message = #Message OUTPUT;
PRINT #Status
PRINT #Message
RETURN
}
I am getting the proper result. Thanks for post.
There may be a more 'correct' answer than this, but one easy way is to just put the two values into one and separate them with some character such as a comma, tab, pipe, etc. So that way you are just returning one value and you can split it on the other end back into 2 values based on the delimeter.

Resources