DB2 SQL procedure - Calling initiate procedure before declaration - stored-procedures

I am making an sql procedure (in lib EXTLIB) that will be called from an external system using JDBC. We have a user, EXTUSER, that is used for running all requests coming from there. The problem is that this user can only have a single starting library, which in our case is EXTLIB (if anyone knows how to add more, please do tell me and become a hero in my company). We have a procedure initiate that initiates the environment (i.e. adds all necessary libs to LIBL). This needs to be called at the start of all procedures in EXTLIB.
My procedure needs to fetch data from a view in our internal lib: INTERLIB. The problem is that we are using ARCAD for version/release/everything management, and ARCAD doesn't like qualifiers, so it automatically removes them, meaning that the declaration at the start can't find the view in INTERLIB, and thus it stops before reaching the initate call.
I need a way to run the call before the declaration, while retaining the functionality. I'm also open to any other alternative solutions. The only one I can think of is instead coding it in RPG, but we have a lot of simple procedures for which that would just be a waste of time. manually creating the procedure in STRSQL is not an option, because when ARCAD transfers a version to production it will recompile everything, thus removing any manual qualifiers we have put there.
Here's the code:
CREATE PROCEDURE MyProcedure ()
LANGUAGE SQL
SPECIFIC MYFILE
CALLED ON NULL INPUT
DYNAMIC RESULT SETS 1
BEGIN
declare c1 cursor with return to client for
select
id,
required
from INTERLIB.cus_CustomerPurpose_v3_0
call INITIATE();
open c1;
call DEINITIATE();
END;

I think you have two options:
1) To create an internal block in the stored procedure,
CREATE PROCEDURE MyProcedure ()
LANGUAGE SQL
SPECIFIC MYFILE
CALLED ON NULL INPUT
DYNAMIC RESULT SETS 1
BEGIN
call INITIATE();
BEGIN
declare c1 cursor with return to client for
select
id,
required
from INTERLIB.cus_CustomerPurpose_v3_0;
open c1;
END;
call DEINITIATE();
END #
2) To call another stored procedure
CREATE PROCEDURE MyProcedure ()
LANGUAGE SQL
SPECIFIC MYFILE
CALLED ON NULL INPUT
DYNAMIC RESULT SETS 1
BEGIN
call INITIATE();
call another_SP();
call DEINITIATE();
END #

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.

Integration through procedure Oracle

I would like to integrate with OSB keeping my logic in the DB. To minimize bandwidth load and calls to DB I would like to batch insert records through a Stored Procedure.
my goal -> avoid loops on procedure, avoid cursor, avoid multi call to DB.
In TSQL I would have done this through Table type parameter, but as this is not possible in PLSQL I have created the following script that gives me error on parameter select in the package body. Can anyone help me?:
--create type object
create or replace TYPE "Type_test" IS OBJECT (
id number(20,0)
oppdateringstype VARCHAR2(50)
)
;
-- Create package head
create or replace package test as
type t_input_table is table of Type_test index by binary_integer;
procedure Proc (p_input_table in t_input_table);
end;
--Create package body
create or replace package body test as
procedure Proc(p_input_table in t_input_table)
as
BEGIN
--Some logic select * from p_input_table
end;

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.

db2 from procedure run file

using IBM DB2 i would like to create a procedure that in case a table does not exist goes to file and start with create and insert statements written in that file. I cannot put all statements inside a procedure because there is just too many of them
my semi-pseudo code so far is
CREATE PROCEDURE KLEMENTEST.create_table
()
LANGUAGE SQL
begin atomic
if (not exists(select 'A' from syscat.tables where tabschema = 'TESTSCHEMA' and tabname='TESTTABLE')) then
--- run queries from file located in "c:\path\to\file.txt"
end if
end ;
any suggestions ??
thank you
There are several options:
1) Create an external stored procedure in Java or C that will execute the command that you want. For example
db2 -tf file.sql
Remember to execute DB2 with a profile (. db2profile). You will call this stored procedure after having test that the table does not exist.
2) Create the stored procedure, and read the content of the file that contains the ddl, then with the content, you create a dynamic SQL. You can read files via UTL_FILE module. However, this module is not available in Express-C edition. http://publib.boulder.ibm.com/infocenter/db2luw/v10r1/topic/com.ibm.db2.luw.apdv.sqlpl.doc/doc/r0053689.html
The general problem of this approach is the location of the file to "execute" in the file system, and also the stored procedure becomes platform dependent.

Calling an oracle stored procedure form COGNOS

In this link IBM they explain how to use an Oracle stored procedure with COGNOS.
They are doing :
create or replace package body project_pk as
procedure project_sp (tproj IN numeric, result1 in out project_type1) is
begin
open result1 for
select projects.projectid, projects.projecttype
from projects
where projects.projecttype=tproj;
end;
end;
/
With open result1 for they are opening the cursor explicitly. But, they do not seem to close it. Why is this?
You must return resuly set to Cognos. If you close the cursor then there are nor reuslts, right?
It's Cognos responsibility to close the cursor, once it finishes to pull the data from the SP.
In order to make you 100% sure that this is the case look at this link (totaly unrelated to Cognos):
Returning result Sets from SQL Server and Oracle
However, the sample you gave in your link, looks quite complex. Here is what I am using:
CREATE OR REPLACE PROCEDURE "COGNOS_SP" (
case_id in numeric,
po_refcur out sys_refcursor) is
BEGIN
open po_refcur for
select * FROM CASES WHERE CASE_ID = case_id;
END COGNOS_SP;

Resources