Send a List Parameter to DB2 Stored Procedure - stored-procedures

I have a db2 stored procedure which has an sql statement using the 'recseq In(1,2,3)' syntax.
I have added a parameter(Character(5) to hold the list but in my call statement I cannot figure out the correct syntax.
I have tried call myprocedure(1, '1,2,3') but this generates a Conversion error
Note: call myprocedure(1, '1') will work but just not the list style.

Related

How can we call stored procedures in Netezza database?

I am not able to call stored procedures in Netezza database.
Below is my procedure.
CREATE OR REPLACE PROCEDURE "SUJAY"."ADMIN"."EMPSELECT"()
RETURNS INTEGER
LANGUAGE NZPLSQL AS
BEGIN_PROC
BEGIN
select * from Employee;
END;
END_PROC;
When I run this in procedure editor in DB visualizer it throws below error.
[Code: 1100, SQL State: HY000] ERROR: Can't have a SELECT without INTO. Use CALL instead.
To resolve this error, you will need to modify your SELECT statement to include an INTO clause, which assigns the result of the SELECT statement to a variable. Here is an example of a SELECT statement that includes an INTO clause: something like this...
SELECT COUNT(*) INTO my_variable FROM mytable WHERE somecondition = false;
If you want to execute the SELECT statement without capturing the result and without an INTO clause, you could use the CALL statement instead of SELECT, this way it will not raise an exception.
CALL EMPSELECT();

Multiple Session executing Snowflake Procedure Insert into same Table

We have a snowflake procedure that insert record into a table, something similar to below procedure. When this procedure is called by multiple application in multiple session, it is behaving erratic and throwing error. I understand when multiple session is trying to write to same table it is throwing error. Is there any work around to handle this scenario, since we do want to duplicate this proc for every application. Thanks in advance.
CREATE OR REPLACE PROCEDURE "SP_CRT_METADATA"(SOURCE_TYPE VARCHAR, BATCH_ID FLOAT)
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
EXECUTE AS OWNER
AS '
hist_col_load =` INSERT INTO SHRD_COL_MSTR_HIST SELECT * FROM SHRD_COL_MSTR WHERE SRC_TYPE =''`+SOURCE_TYPE+`'' AND ACTIVE_IND =''Y''`;
snowflake.execute({sqlText: hist_col_load});
'

How to assign default property value for IN parameters in teradata stored procedure

Need to develop a stored procedure to insert values into a table. Consider the table has 5 columns, where not all columns values are mandatory for user inputs. The DDL for stored procedure will be like
CREATE OR REPLACE PROCEDURE UPDATE_EMPLOYEE_INFO
(IN EMPLOYEE_NUMBER CHAR(10),
IN EMP_DEPT CHAR(3),
IN PHONE_NUMBER CHAR(4),
IN JOB CHAR(8),
IN ELEVEL SMALLINT)
Begin
--- Insert query here ---
End
This procedure can be execute using commands
CALL UPDATE_EMPLOYEE_INFO(1,'',1234567890,'admin','')
However,columns EMP_DEPT and ELEVEL are not mandatory fields to have values. How can I mention in stored procedure call to take default values like below.
CALL UPDATE_EMPLOYEE_INFO(1,DEFAULT,1234567890,'admin',DEFAULT).
Basically, I want to achieve something kind of this in link using teradata - https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/sqlp/rbafyprocdefaults.htm
There are no optional parameters. All parameters are mandatory. So you would probably have to pass in optional paramters as null and then write some conditional logic into your stored proc based on whether the parameter is null.
For example:
CALL UPDATE_EMPLOYEE_INFO(1,NULL,1234567890,'admin',NULL)
The Teradata documentation says:
Rules For Specifying Input And Output Parameters
Call arguments consisting of input and output parameters must be
submitted with a CALL statement. No default parameter values can be
defined at the time a procedure is created; the CALL returns an error
if the required call arguments are not specified.
https://info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference/B035-1146-160K/cum1472240816735.html

DB2 calling stored Procedure in conditional statement

I need to query a stored procedure and on the basis of result set of that procedure need to make decisions in conditional statement
For instance I have a stored Procedure "Main_SP"
Now if result of "Main_SP" is 'null' then the result should be 'Tweety', but if th result set is not null then result set should be retrieved,
how to do it?
I tried following and some other but none worked.
SELECT
case Main_SP('MyVariable')
when 'null'
then 'Tweety' end
FROM SYSIBM.SYSDUMMY1 WITH UR
SELECT
case Main_SP('MyVariable')
when null
then 'Tweety' end
FROM SYSIBM.SYSDUMMY1 WITH UR
It is failing the condition, In first command when even it is 'null' it is not printing 'Tweety'.
and while using second, Getting error that 'Null' is not valid in the context.
I don't believe you can use a stored procedure that way.
A stored procedure can return data in two ways
As a result set
in an output or input/output parm
You're not using option 2 and I don't think a result set is ever NULL. There might be no records, but the RS itself isn't NULL. In addition, from what I can see in the manuals, processing a RS from a stored proc inside another stored proc in DB2 requires you to declare an allocate result set locators. But I've never done this.
If your procedure returns a single value or null, you'd be better served by defining it as a function with a return value. Then your code would be simply:
SELECT
COALESCE(Main_Fnc('MyVariable'),'Tweety')
FROM
SYSIBM.SYSDUMMY1 WITH UR
You can't call a stored procedure as part of an SQL fullselect; you would need to have the client that calls the stored procedure handle this logic. If you must do it on the server, implement a new stored procedure that calls Main_SP itself and contains your logic to interpret the result.

how do i execute a stored procedure with vici coolstorage?

I'm building an app around Vici Coolstorage (asp.net version). I have my classes created and mapped to my database tables and can pull a list of all records fine.
I've written a stored procedure where the query jumps across databases that aren't mapped with Coolstorage, however, the fields in the query result map directly to one of my classes. The procedure takes 1 parameter.
so 2 questions here:
how do i execute the stored procedure? i'm doing this
CSParameterCollection collection = new CSParameterCollection();
collection.Add("#id", id);
var result = Vici.CoolStorage.CSDatabase.RunQuery("procedurename", collection);
and getting the exception "Incorrect syntax near 'procedurename'." (i'm guessing this is because it's trying to execute it as text rather than a procedure?)
and also, since the class representing my table is defined as abstract, how do i specify that result should create a list of MyTable objects instead of generic or dynamic or whatever objects? if i try
Vici.CoolStorage.CSDatabase.RunQuery<MyTable>(...)
the compiler yells at me for it being an abstract class.
There's a shortcut in CoolStorage to run a stored procedure. Simply prefix the stored procedure name with "!":
CSDatabase.RunQuery("!procedurename", collection);

Resources