How to call stored procedure inside another stored procedure and set the values as in variable in DB2 - sql

I need to call stored procedure into another stored procedure and return value stored in variable in db2.
Thanks

One examples could be this one:
DECLARE STMT STATEMENT;
PREPARE STMT FROM 'CALL MODIFY_DESCENDANTS (?, ?)';
EXECUTE STMT USING LOG_ID, LEVEL;
Taken from log4db2 - https://github.com/angoca/log4db2/blob/master/src/main/sql-pl/03-UtilityBody.sql#L729
If the stored procedure returns a result set, and you want to take values from that, then you need to call the stored procedure and use the associate and allocate statements. For an example, you can visit https://www.toadworld.com/platforms/ibmdb2/w/wiki/7460.call-allocate-and-associate
CALL EMPR;
ASSOCIATE RESULT SET LOCATOR (LOC1) WITH PROCEDURE EMPR;
ALLOCATE C1 CURSOR FOR RESULT SET LOC1;

Related

Wrapping procedure calls in one main procedure

I have four working stored procedures (DB2), each accepting their own parameters, but a lot of those params are the same.
I'm trying to create a larger wrapper procedure that will basically just call each in order. There are about 10 outside values coming into the wrapper procedure as it's own parameters.
Within the body, where I call all 4 child parameters, I will be using a mixture of outside params as well as a main ID param that is derived from the first call.
How can I properly wrap these four procedure calls using the outside params as well as the captured/derived parameter from my first call?
Parameters:
FIRST_NAME
LAST_NAME
PROFILE_IMAGE
CODE
START_DATE
EXPIRE_DATE
PRIORITY
CUST_NUMBER
CONTACT_TYPE
CONTACT_VALUE
CALL FIRST_PROC(FIRST_NAME,LAST_NAME,PROFILE_IMAGE)
--This returns ```FIRST_PROC_ID```
CALL SECOND_PROC(FIRST_PROC_ID, CODE,START_DATE,EXPIRE_DATE,PRIORITY)
CALL THIRD_PROC(FIRST_PROC_ID,CODE,CUST_NUMBER,START_DATE,EXPIRE_DATE,PRIORITY)
CALL FOURTH_PROC(FIRST_PROC_ID,CONTACT_TYPE,CONTACT_VALUE,START_DATE,EXPIRE_DATE)
Use the GET DIAGNOSTICS statement.
--#SET TERMINATOR #
SET SERVEROUTPUT ON#
CREATE OR REPLACE PROCEDURE TEST1(P_FIRST_PROC_ID INT)
BEGIN
RETURN P_FIRST_PROC_ID;
END#
BEGIN
DECLARE V_RC INT;
CALL TEST1(10);
GET DIAGNOSTICS V_RC = DB2_RETURN_STATUS;
CALL DBMS_OUTPUT.PUT_LINE('Return Status: '||V_RC);
END#

Teradata Dynamic Insert - Looped?

I'm doing something very similar to dnoeth's second comment on this question:
Insert Into table Teradata dynamic stored procedure SQL
I need to run it multiple times to loop the same insert statement but with different values for the "?" and I'm not sure how to go about that.
The dynamic value in my version is a date span. I can't run a massive insert without spooling out so I've broken the data into segments.
Thanks.
William,
As you are aware that you need to use cursor to iterate through each value you want to process.
For that purpose store dynamically created INSERT statement into a variable.
Final step: Use EXECUTE IMMEDIATE command to run insert statement stored in teradata variable
Here is the working sample that you may refer
REPLACE PROCEDURE td_user.sp_dynamic_insert( OUT proc_msg VARCHAR(5000) )
BEGIN
DECLARE lv_insert_txt VARCHAR(20000);
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
/* Error handling code if required */
END;
L0:
FOR insert_cursor AS select_list
CURSOR FOR
SELECT Col2
FROM test_1
DO
SET lv_insert_txt = 'INSERT INTO test_2(Col1,Col2) VALUES('||TRIM(insert_cursor.Col2)||','||TRIM(insert_cursor.Col2)||')';
EXECUTE IMMEDIATE lv_insert_txt;
END FOR L0;
SET proc_msg = 'Procedure completed successfully';
END;

Variable must be declared error in a stored procedure

I have a package whose body consists of a stored procedure.
The stored procedure makes a call to a stored procedure in another package. However I need to pass the result of a select statement as a parameter. so I stored the result in a variable and passed it as a parameter. However when the procedure call is performed, it says, the variable is not declared.
This is the sample code.
create or replace
PACKAGE BODY PKG_BODY
PROCEDURE PROC (error OUT VARCHAR2,
RESULT OUT BOOLEAN ) AS
-----------------------------
-----------------------------
DECLARE
PARAM TABLE1.COLUMN1%TYPE;
BEGIN
SELECT COLUMN1
INTO PARAM
FROM TABLE1
WHERE COLUMN2=ID AND COLUMN3='NAME';
END;
PUBLISH_MSG.PUBLISH_PMM_MSG('ID',PARAM,PO_ERR_MSG); --another procedure call
END PROC;
I am getting the following error :
PLS:00201 'Identifier PARAM must be declaed'
Do I have to declare the PARAM before somewhere else? Please help !!
You have defined that variable in a nested PL/SQL block - it can't be accessed from an outer block (it doesn't even exist after the block is finished). You should move definition of your param variable to the outer block, for example:
PROCEDURE PROC (error OUT VARCHAR2,
RESULT OUT BOOLEAN )
AS
PARAM TABLE1.COLUMN1%TYPE;
BEGIN
-----------------------------
-----------------------------
SELECT COLUMN1
INTO PARAM
FROM TABLE1
WHERE COLUMN2=ID AND COLUMN3='NAME';
PUBLISH_MSG.PUBLISH_PMM_MSG('ID',PARAM,PO_ERR_MSG); --another procedure call
END PROC;

Dynamic SQL not working as expected

create or replace procedure createtables
Authid current_user as
begin
execute immediate 'create table newcustomer as select * from customer';
end;
create or replace procedure e
is
begin
createtables;
select * from newcustomer;
end;
I got two procedures above. first one will create a new tables called newcustomer, second procedure will call the first procedure and query to the newcustomer table. when I try to compile this code, it says the table is not yet created, I don't really get it as I have called createtables procedure so I assume I have created the table.
Any help will be appreciated. Thanks
Compiling the second procedure without executing the first procedure first will fail, since the table has not been created.
You cannot compile a procedure that relies on objects that do not exist.
Use EXEC createtables before creating procedure e, and do not call createtables in there.
Procedure e will also not compile because you are not using the results of select * from newcustomer as cursor or store the results into variables.
EDIT:
Instead of procedures, you could use an anonymous block. Put the following into a file and execute it (via SQL*Plus for example):
Create Table newcustomer As Select * From customer;
Begin
Null; --# Do something with your new table in here.
End;
/

How to Execute stored procedure from SQL Plus?

I have a stored procedure in oracle and want to test it from SQLPlus.
If I use
execute my_stored_proc (-1,2,0.01)
I get this error
PLS-00306: wrong number or types of arguments in call to my_stored_proc
The beginning for the proc is this
create or replace PROCEDURE my_stored_proc
( a IN NUMBER,
b IN NUMBER,
c IN NUMBER,
z out NUMBER
) AS ....
Do I need to provide the a var for the out parameter, is so how? I tried:
var z NUMBER;
But get this error when I try to run the proc
execute my_stored_proc (-1,2,0.01,z)
PLS-00201: identifier 'Z' must be declared
Also when I was in SQL-Developer it gave me the usage and it show the inputs in reverse order, that is:
execute my_stored_proc(z number,c number,b number,a number);
Do you provide them in reverse order or is that just something with SQL-Developer
I did not write the procedure and I don't normally deal with them so I could be missing something obvious.
Thanks
You have two options, a PL/SQL block or SQL*Plus bind variables:
var z number
execute my_stored_proc (-1,2,0.01,:z)
print z
You forgot to put z as an bind variable.
The following EXECUTE command runs a PL/SQL statement that references a stored procedure:
SQL> EXECUTE -
> :Z := EMP_SALE.HIRE('JACK','MANAGER','JONES',2990,'SALES')
Note that the value returned by the stored procedure is being return into :Z