Use of variable SET statement in stored procedures compiles but fires exception at runtime - stored-procedures

In a Snowflake SQL-stored procedure, I would like to SET value of a variable like the following:
DROP PROCEDURE IF EXISTS MY_DB.MY_SCHEMA.MY_SP_SQL();
CREATE PROCEDURE MY_DB.MY_SCHEMA.MY_SP_SQL()
RETURNS STRING NOT NULL
LANGUAGE SQL
AS
DECLARE
retValue VARCHAR;
BEGIN
SET retVal = '';
[...]
Everything rocks at compile time but fails at runtime with the following exception
Uncaught exception of type 'STATEMENT_ERROR' on line x at position y :
Stored procedure execution error: Unsupported statement type 'SET'.
despite documentation says this to be possible ๐Ÿคจ
Can someone gently explain to the noob of me? ๐Ÿ˜
Thanks,

Within a SQL stored procedure use LET instead of SET. The syntax for stored procedures (also known as Snowflake Scripting) is different from interactive SQL. The valid commands in SQL scripting are described in the Snowflake Scripting Reference

Related

How to call external stored procedure from SQL stored procedure and handle commitment control

I am trying to call a external stored procedure from Sql stored procedure by passing two parameters. one of the parameters is expected to return back with a string value, which when arrives I need to stop the data to be committed in any file.
To explain the situation - SP1 (SQL stored procedure) will call SP2 (External stored procedure), which will call RPGLE program PGM1, which will call another RPGLE program PGM2.
Now I am tasked to handle commitment control of File1 and File 2 used in PGM2 from SP1. If at any point File1 is updated and File2 gives an error while updating any record, data from File 1 should also be rolled back. but this rollback should happen in SP1.
So far I have tried to split this issue in two parts-
PARTA - How to Call External stored procedure from SQL stored procedure.
PARTB - How to handle commitment in SQL stored procedure in essence, if PGM2 gives back error data should be rolled back.
Below is the piece of code so far I have tried. But have no luck.
CREATE OR REPLACE PROCEDURE MYLIB.SP1 (
IN PRINPUT CHAR(1200) ,
INOUT PRERR CHAR(50) )
SPECIFIC MYLIB.SP1
BEGIN
DECLARE SQLSTATE CHAR(5) DEFAULT ' ';
DECLARE SQLCODE INTEGER DEFAULT 0;
CALL MYLIB.SP2(PRINPUT, PRERR);
IF SQLCODE = 0 THEN
COMMIT;
ENDIF;
END
Any suggestion/Guidance is appreciated.
Try this my friend:
On SP1
BEGIN
DECLARE SQLSTATE CHAR(5) DEFAULT ' ';
DECLARE SQLCODE INTEGER DEFAULT 0;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
CALL MYLIB.SP2(PRINPUT, PRERR);
IF SQLCODE = 0 THEN
COMMIT;
ELSE
ROLLBACK;
ENDIF;
END
Answers:
PARTA: If the Ext Stored Proc exists, the way you're calling it, it's correct.
PARTB: The set transaction scopes all the rows changed from the moment is issued, to the execution of commit or rollback. Remember this, every SQL Stored proc runs on *caller actgrp, so, you need to check if your RPG program runs on *caller too.
Finally, last time I tested, dinos still walked the earth, the commit on SQL Stored Proc scoped the changes made with a RPG program called within, but the RPG STRCMTCTL doesn't get the changes made on SQL Stored Proc called within the RPG.
Have fun!
statements-set-transaction
control-example-using-transaction-logging-file-start-application
definition-example-jobs-commitment-definitions
Thank you #Jairo, I got it working.
Your solution was correct and it worked, only challenge was the SP2 didn't have any SQL statements which was causing issue, I converted the statements in SP2 to SQL statements and it worked completely fine.

Azure logic apps error while calling oracle stored procedure with output parameter

My stored procedure parameters look like this
PROCEDURE PR_NAME
(
fromdate IN NVARCHAR2,
todate IN NVARCHAR2,
myCur OUT SYS_REFCURSOR
)
I call the procedure in sql developer client like this
var c refcursor;
execute PR_NAME ('01-01-2019','01-01-2019 10:00',:c);
print c;
I am trying to create API which call the stored procedure in Logic apps by passing fromdate and todate. But I am getting an error as below.
"Database functions with non-scalar parameters aren't supported.\r\n inner exception: Database functions with non-scalar parameters aren't supported.\r\"
Do someone know why??
There is already a workitem for this and can be tracked in Github. Database functions with non-scalar parameters aren't supported.
https://github.com/MicrosoftDocs/azure-docs/issues/24266

Display result of a variable in teradata stored procedure

I need to print results of variables in Teradata stored procedures, but print does not work in teradata, should I use the below method or please suggest the proper one:
My code:
REPLACE PROCEDURE Name()
---some code goes here
BEGIN
TRANSACTION;
SET var1= var2+ 3;
SET var3= var2;
/* !!! PRINT var1*/
/* !!! PRINT var3*/
end;
--some code here
I used the out in stored procedure declaration, suggest me another way to print the values of variable:
REPLACE PROCEDURE Name(out var1, out var3)
As long as there's only a single line of values has to be returned you can utilize OUT variables.
For multiple lines you can use a Volatile Table where those lines are inserted and a final DYNAMIC RESULT SET returns them as an answer set.
If you want to print the value of a variable inside a stored procedure I assume you are asking in the context of debugging the store procedure. Teradata doesn't appear to have the debugging tricks that other databases provide, e.g. RAISE and NOTICE. One workaround is the create a log table with at least one column, message VARCHAR(500) and then insert your debugging messages into that table.

How to define an Array of values (or a Column) into a Procedure Argument?

I am working on a Netezza SP and is stuck with a problem.
I have a SP, defined as say:
CREATE OR REPLACE PROCEDURE MY_PROC(VARCHAR(ANY)) RETURNS INTEGER LANGUAGE NZPLSQL
AS
BEGIN_PROC
DECLARE
v_temp ALIAS FOR $1;
/* Other decalarations */
result_ts INTEGER;
BEGIN
result_ts := 0;
/* Procedure Body */
RETURN result_ts;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'Exception Raised: %', SQLERRM;
END;
END_PROC;
If I am running this SP with one value, such as:
SELECT MY_PROC('TEST_INPUT');
But if I am trying to run it with a column value, such as:
SELECT MY_PROC(TEST_COLUMN) FROM TEST_TABLE;
Its giving me error as:
ERROR: Can't use a stored procedure in this context
I know that in the second scenario I am passing an Array (i guess) but this is not what the Procedure has expected.
Now I am trying to have a procedure that can accept these kind of values but could not succeeded so far, LOOPing and all I have taken care but only problem is the Argument which I don't know how to pass.
Any help would be appreciated, let me know if I need to provide any extra info on this.
Asif
Stored procedures in Netezza, as of v7.2, can only be called in the following ways, as documented here.
CALL sproc_name(...);
EXEC sproc_name(...);
SELECT sproc_name(...);
Note that the SELECT form does not allow a FROM clause.
If you want the stored procedure to act on a particular column from a particular table that changes from invocation to invocation, you could pass the names of those as arguments to the stored procedure and have the entirety of the SQL logic encoded within. You could even pass arbitrary code into the stored procedure to build a query internally.
The way you are trying to call it now is more like calling a user defined function, and that simply won't work with stored procedures here.

Simple Oracle Stored Procedure getting Invalid object error

I am trying to write a simple Oracle Stored Procedure:
CREATE OR REPLACE PROCEDURE act.skeleton
IS
DECLARE
v_rowCount NUMBER;
BEGIN
SELECT COUNT(1) INTO v_rowCount FROM ex.emp;
DBMS_OUTPUT.PUT_LINE(v_rowCount);
END;
However, when I try & run the procedure by issuing execute act.skeleton in PL/SQL Developer command window, I get the following error message:
ORA-06550: line 1, column 11:
PLS-00905: object ACT.SKELETON is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I tried to just run the code without the create procedure statement & it runs successfully with the output being displayed. I have tried both CREATE OR REPLACE PROCEDURE IS & CREATE OR REPLACE PROCEDURE AS options but I still get the same error.
I am not sure if this has anything to do with authorization or visibility of the procedure when I try and execute it or what is causing the act.skeleton object to be invalid. I looked into what the error means & it seems it usually refers to compilation error in the procedure but since I can run the code sans the procedure declaration, I am guessing the declaration piece is the culprit. I just don't know enough to figure out what is causing this and I was hoping that someone will be able to shed some more light on it and point me in the right direction
Thanks,
Ashish
Other Details:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
Don't use the DECLARE keyword in a stored procedure declaration. Remove that and it should compile.

Resources