Switch role in Snowflake Procedure - snowflake-cloud-data-platform

Cant switch role in Javascript Procedure.
In my Snowflake Javascipt Procedure i try the command:
snowflake.execute( {sqlText: "use role sysadmin" });
Stored procedure execution error:
Unsupported statement type 'USE'.
At Statement.execute, line 49 position 13
Any sugestions?

Try executing it as caller:
CREATE OR REPLACE PROCEDURE useroletest()
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
snowflake.createStatement( {sqlText: `USE ROLE SECURITYADMIN;`} ).execute();
$$
;

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.

Execute stored procedure inside another stored procedure in snowflake

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();

using session variables while deploying stored procedures-snowflake

I have a situation where I am pushing my stored procedure DDL into git repository. These codes have some environment specific codes, like I have dev,qa and prd, while I need to replace every time while deploying.
What I am looking for is to be able to use variables, that could be replaced while deploying. Something like this
var env= 'dev';
create or replace procedure test
returns varchar
language javascript
as
$$
insert into raw_$env.employee values(1);
$$;
When I run this code I want to have the code deployed like this.
create or replace procedure test
returns varchar
language javascript
as
$$
insert into raw_raw.employee values(1);
$$;
To specify the dynamic schema name, you can use IDENTIFIER(). Then use the javascript stored procedure API to execute.
create or replace procedure test()
returns text
language javascript
execute as caller
as
$$
snowflake.createStatement({ sqlText: `set t = 'raw_' || $env || '.employee'` }).execute()
snowflake.createStatement({ sqlText: `insert into identifier($t) values(1)` }).execute()
return 'SUCCESS'
$$;
set env= 'dev';
call test();
You may want to specify the database as well.
UPDATE -- Static solution, hard coding the 'env' parameter
RUN IN SNOWSQL
!define env=dev;
create or replace procedure test()
returns text
language javascript
execute as caller
as
$$
snowflake.createStatement({ sqlText: `insert into raw_&env.employee values(1)` }).execute() return 'SUCCESS'
$$;
To verify that the substition is static:
select get_ddl('procedure','test()')
The env parameter can also be defined in a config file or on the SNOWSQL command line.

Snowflake How to execute remove file in stage through a stored procedure?

How to execute rm #mystage/data/input/ pattern='.*success.*'; from a stored proc
I'm getting an error message "Stored procedure execution error: Unsupported statement type 'UNKNOWN'."
The following procedure works for me
CREATE OR REPLACE PROCEDURE RM_FROM_STAGE() RETURNS STRING LANGUAGE JAVASCRIPT AS
$$ snowflake.execute({sqlText: `rm #mystage/test/ pattern='.*success.*'` }); $$;
Maybe all you need are the backquotes...

Resources