Snowflake How to execute remove file in stage through a stored procedure? - snowflake-cloud-data-platform

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...

Related

Procedure is not getting created in postgres11

I am getting below error while creating simple stored procedure in postgresql-11,
ERROR: syntax error at or near "create"
LINE 4: create or replace procedure transfer(
How to solve this error?
See the screenshot for more info.
As ran in the query tool got few syntax errors after rewriting as below executed successfully:
create or replace procedure transfer()
language plpgsql
as $$
DECLARE
begin
Select now();
END $$;

Execute multiple stored procedures with one input parameter SSIS

I am trying to execute multiple stored procedures using only one input parameter in SSIS using Execute SQL Task but I keep getting this error:
[Execute SQL Task] Error: Executing the query "EXEC sample_stored_proc1..." failed with the following error: "Value does not fall within the expected range.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Here is a sample code inside SQL TASK for calling the stored procedures:
EXEC sample_stored_proc1 ?;
EXEC sample_stored_proc2 ?;
EXEC sample_stored_proc3 ?;
NOTES
I tried the code with only 1 stored procedure and it works but adding 1 or more stored procedures is giving me the above error.
using OLE DB connection
I found one solution, I created a new Stored Procedure with an input parameter and put this in EXECUTE SQL TASK instead. this will be used to execute all the stored procedures above.
ALTER PROCEDURE [dbo].[xp_EXEC_ALL_sample_SPs] #Start_Date as DATE
AS
BEGIN
EXECUTE sample_stored_proc1 #Start_Date;
EXECUTE sample_stored_proc2 #Start_Date;
EXECUTE sample_stored_proc3 #Start_Date;
END
Please let me know if there are better solution
when you execute a stored procedure it returns a execution status, so when you ran three procedure it returned multiple result sets, you can simply ignore the result set returned by
DECLARE #result int;
DECLARE #start_date DATE = ?;
EXEC #result = sample_stored_proc1 #start_date;
EXEC #result = sample_stored_proc2 #start_date;
EXEC #result = sample_stored_proc3 #start_date;
doing so will suppress multiple result sets from the query

Can we Use Snowflake Tables functions in the procedure?

Experts,
I am trying to use tables function inside the snowflake procedure. However it throws me an error.
CREATE PROCEDURE
.....
....
extcnt ="select count(*) from table(information_schema.external_table_files(table_name=>''MYTABLE''))";
snowflake.execute({sqlText: extcnt});
.....
I get below error message. Is there any restriction in using table function inside the procedure? Please Help.
"JavaScript execution error: Uncaught 100183-Stored procedure execution error: Requested information on the current user is not accessible in stored procedure"
Regards,
Gopi
Check the "Caller’s Rights and Owner’s Rights Stored Procedures" - this is probably the source of that error message:
https://docs.snowflake.com/en/sql-reference/stored-procedures-rights.html
When defining a stored procedure you can ask it to EXECUTE AS CALLER, which changes the access it has to different resources.
Create a stored procedure as an owner’s rights stored procedure if all of the following are true: You want to delegate a task(s) to another user(s) who will run with the owner’s privileges, not the caller’s own privileges [...]
Create a stored procedure as a caller’s rights stored procedure if the following are true: The stored procedure operates only on objects that the caller owns or has the required privileges on [...]
CREATE PROCEDURE sv_proc1()
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
...

System.Data.SqlClient.SqlException procedure has no parameters and arguments were supplied. How should I go about fixing this error?

I built a database application in Visual Studio. When I run, I get this error:
System.Data.SqlClient.SqlException: procedure sp_select_number_of_films has no parameters and arguments were supplied.
I know what the error means, but I am not sure how to fix it in my SQL code. I have tried a bunch of small tweaks, but I still get the same error.
Stored procedure:
CREATE PROCEDURE [dbo].[sp_select_number_of_films]
(#REC_ID_OUTPUT int OUTPUT)
AS
SELECT COUNT(*)
FROM Film.Title;
EXEC dbo.sp_select_number_of_films
GO
It should be called as:
DECLARE #sth INT;
Exec dbo.sp_select_number_of_films #sth OUTPUT;
But then inside stored procedure you need to assign output variable:
CREATE PROCEDURE [dbo].[sp_select_number_of_films]( #REC_ID_OUTPUT int OUTPUT)
AS SELECT #REC_ID_OUTPUT = COUNT(*)
FROM Film.Title;
And you should avoid naming stored procedures with "sp_" prefix.

Create user through procedure on another database via database link

I'd like to create users through procedure on another database via database link.I am getting error while executing procedure.
Here are the code which i used.
create or REPLACE PROCEDURE hostname
(host_name in varchar2,user_name in VARCHAR2, pass_word in VARCHAR2,
table_space in varchar2,pro_file in varchar2)
as
db_link_name varchar2(30);
begin
select db_link into db_link_name from all_db_links where host=host_name;
EXECUTE IMMEDIATE 'dbms_utility.exec_ddl_statement#'||db_link_name||('CREATE USER '||user_name||' IDENTIFIED BY '||pass_word||'
DEFAULT TABLESPACE '||table_space||' PROFILE '|| pro_file||' ACCOUNT UNLOCK');
EXECUTE IMMEDIATE 'dbms_utility.exec_ddl_statement#'||db_link_name||
('GRANT CONNECT,RESOURCE,EXECUTE_CATALOG_ROLE,Create table,create session,create view,create sequence,create procedure,create job,create synonym TO '||user_name);
end;
/
execute hostname('orcl1','rahul1','rahul','users','default');
Error: ORA-00900: invalid SQL statement ORA-06512: at
"SYS.HOSTNAME", line 6 ORA-06512: at line 1
00900. 00000 - "invalid SQL statement"
*Cause:
*Action:
I think you may have got a little confused by some of the advice provided by the commenter mustaccio. What he meant to say is that the SQL strings in your EXECUTE IMMEDIATE statements need to use PL/SQL blocks in order to call stored procedures.
In other words, instead of writing
EXECUTE IMMEDIATE 'dbms_utility.exec_ddl_statement ... ';
you should write
EXECUTE IMMEDIATE 'BEGIN dbms_utility.exec_ddl_statement ... ; END;';
For your procedure, I would recommend doing introducing a local variable for the strings you are passing to EXECUTE IMMEDIATE. These can be notoriously tricky to get right, and if you've assigned them to a local variable you can easily output them using dbms_output.put_line. In fact, I did exactly this while fixing up the problems in your procedure. I would recommend continuing to do this if you wish to modify or extend your procedure.
Anyway, here's what I ended up with, which appeared to work:
create or REPLACE PROCEDURE hostname
(host_name in varchar2,user_name in VARCHAR2, pass_word in VARCHAR2,
table_space in varchar2,pro_file in varchar2)
as
db_link_name varchar2(30);
l_ddl_sql varchar2(4000);
begin
select db_link into db_link_name from all_db_links where host=host_name;
l_ddl_sql := 'begin dbms_utility.exec_ddl_statement#'||db_link_name||'(''CREATE USER '||user_name||' IDENTIFIED BY '||pass_word||'
DEFAULT TABLESPACE '||table_space||' PROFILE '|| pro_file||' ACCOUNT UNLOCK''); END;';
EXECUTE IMMEDIATE l_ddl_sql;
l_ddl_sql := 'begin dbms_utility.exec_ddl_statement#'||db_link_name||
'(''GRANT CONNECT,RESOURCE,EXECUTE_CATALOG_ROLE,Create table,create session,create view,create sequence,create procedure,create job,create synonym TO '||user_name || '''); END;';
EXECUTE IMMEDIATE l_ddl_sql;
end;
/

Resources