Wrapping procedure calls in one main procedure - sql

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#

Related

Return of mysql procedure using golang

I'm having a problem getting the return of a procedure using golang. If I remove the parameters from the procedure the procedure is executed normally, however, when I need to get the return (OUT parameter) I can not get it and the procedure is not executed. To make the tests simpler and to get help, I created a simple procedure as below and I want to get the return of it.
Follow procedure below:
drop procedure if exists PESSOA_TESTE;
delimiter $$
create procedure PESSOA_TESTE(out psaida int)
begin
set psaida = 2;
end
$$
Now it follows the section of golang code that I'm using to try to get the value 2 specified in the procedure.
var GerenciaBD GERENCIABD
var PontoExecucao int
GerenciaBD.F_GERENCIABD_ABRIR_CONEXAO_MYSQL()
GerenciaBD.DataBase.ExecContext(context.TODO(),"call PESSOA_TESTE", sql.Named("psaida", sql.Out{Dest:&PontoExecucao}))
println(PontoExecucao)
When executing I have as answer 0 and not 2, which is the one specified in the procedure.
My golang version is at 1.10.
You can see here that usage of out parameter is not yet implemented for https://github.com/go-sql-driver/mysql library.
I'm not sure what you want to do with your code, but if you want to return only one value, you can do something like this:
Stored procedure:
drop procedure if exists PESSOA_TESTE;
delimiter $$
create procedure PESSOA_TESTE(IN psaida int)
begin
SET psaida = 2;
select psaida;
end
$$
Code:
var GerenciaBD GERENCIABD
var PontoExecucao int
GerenciaBD.F_GERENCIABD_ABRIR_CONEXAO_MYSQL()
GerenciaBD.DataBase.QueryRowContext(context.TODO(),"CALL PESSOA_TESTE(?)", PontoExecucao).Scan(&PontoExecucao)
println(PontoExecucao)

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

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;

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;
/

What is the difference between function and procedure in PL/SQL?

What is the difference between function and procedure in PL/SQL ?
A procedure does not have a return value, whereas a function has.
Example:
CREATE OR REPLACE PROCEDURE my_proc
(p_name IN VARCHAR2 := 'John') as begin ... end
CREATE OR REPLACE FUNCTION my_func
(p_name IN VARCHAR2 := 'John') return varchar2 as begin ... end
Notice how the function has a return clause between the parameter list and the "as" keyword. This means that it is expected to have the last statement inside the body of the function read something like:
return(my_varchar2_local_variable);
Where my_varchar2_local_variable is some varchar2 that should be returned by that function.
A function can be in-lined into a SQL statement, e.g.
select foo
,fn_bar (foo)
from foobar
Which cannot be done with a stored procedure. The architecture of the query optimiser limits what can be done with functions in this context, requiring that they are pure (i.e. the same inputs always produce the same output). This restricts what can be done in the function, but allows it to be used in-line in the query if it is defined to be "pure".
Otherwise, a function (not necessarily deterministic) can return a variable or a result set. In the case of a function returning a result set, you can join it against some other selection in a query. However, you cannot use a non-deterministic function like this in a correlated subquery as the optimiser cannot predict what sort of result set will be returned (this is computationally intractable, like the halting problem).
In dead simple way it makes this meaning.
Functions :
These subprograms return a single value; mainly used to compute and return a value.
Procedure :
These subprograms do not return a value directly; mainly used to perform an action.
Example Program:
CREATE OR REPLACE PROCEDURE greetings
BEGIN
dbms_output.put_line('Hello World!');
END ;
/
Executing a Standalone Procedure :
A standalone procedure can be called in two ways:
• Using the EXECUTE keyword
• Calling the name of procedure from a PL/SQL block
The procedure can also be called from another PL/SQL block:
BEGIN
greetings;
END;
/
Function:
CREATE OR REPLACE FUNCTION totalEmployees
RETURN number IS
total number(3) := 0;
BEGIN
SELECT count(*) into total
FROM employees;
RETURN total;
END;
/
Following program calls the function totalCustomers from an another block
DECLARE
c number(3);
BEGIN
c := totalEmployees();
dbms_output.put_line('Total no. of Employees: ' || c);
END;
/
Both stored procedures and functions are named blocks that reside in the database and can be executed as and when required.
The major differences are:
A stored procedure can optionally return values using out parameters, but can also be written in a manner without returning a value. But, a function must return a value.
A stored procedure cannot be used in a SELECT statement whereas a function can be used in a SELECT statement.
Practically speaking, I would go for a stored procedure for a specific group of requirements and a function for a common requirement that could be shared across multiple scenarios. For example: comparing between two strings, or trimming them or taking the last portion, if we have a function for that, we could globally use it for any application that we have.
The following are the major differences between procedure and function,
Procedure is named PL/SQL block which performs one or more tasks. where function is named PL/SQL block which performs a specific action.
Procedure may or may not return value where as function should return one value.
we can call functions in select statement where as procedure we cant.
In the few words - function returns something. You can use function in SQL query.
Procedure is part of code to do something with data but you can not invoke procedure from query, you have to run it in PL/SQL block.
we can call a stored procedure inside stored Procedure,Function within function ,StoredProcedure within function but we can not call function within stored procedure.
we can call function inside select statement.
We can return value from function without passing output parameter as a parameter to the stored procedure.
This is what the difference i found. Please let me know if any .