DB adapter with stored procedure in OSB - stored-procedures

Appreciate,if anybody help me out with this.
I want to select a record from DB using a DB adapter in OSB and for that i have written a stored procedure in oracle which is like below:-
create or replace
PROCEDURE TRY_POC(ERROR_STATUS IN varchar2,TYPE_ERROR IN varchar2,ERROR_TIMESTAMP IN varchar2,p_cur out sys_refcursor) IS
BEGIN
open p_cur for
select * from CommonLogTable where STATUS=ERROR_STATUS
and ERROR_TYPE=TYPE_ERROR
and
to_date(
(
substr(Error_Timestamp,9,2)||'-'||
substr(Error_Timestamp,6,2)||'-'||
substr(Error_Timestamp,1,4)||' '||
substr(Error_Timestamp,12,2)||':'||
substr(Error_Timestamp,15,2)||':'||
substr(Error_Timestamp,18,2)
),'DD-MM-YYYY HH24:MI:SS') < to_date(to_char(current_timestamp,'DD-MM-YYYY HH24:MI:SS'),'DD-MM-YYYY HH24:MI:SS')
AND to_date(
(
substr(Error_Timestamp,9,2)||'-'||
substr(Error_Timestamp,6,2)||'-'||
substr(error_timestamp,1,4)||' '||
substr(Error_Timestamp,12,2)||':'||
substr(Error_Timestamp,15,2)||':'||
substr(Error_Timestamp,18,2)
),'DD-MM-YYYY HH24:MI:SS') >= to_date(to_char(current_timestamp-(5 * (1/24/60)),'DD-MM-YYYY HH24:MI:SS'),'DD-MM-
YYYY HH24:MI:SS');
end TRY_POC;
What this stored procedure is doing that it is fetcing te records from CommonLoTable based upon the Status,Error_Type and Error_Timestamp(which will be passed at runtime),and fetch the records which lies between
Current Timestamp=Current Timestamp-5
And the error Timestamp Format i need is like 2016-08-13T16:30:00.
I want to pass the values of all Error_Type,Status and Error_Timestamp at runtime.I am not able to pass the value of Error_Timestamp at runtime from OSB .
Thanks in Advance

You can use xquery function execute-sql() in pipeline and call your sql statements. Input parameters can be accepted at runtime from proxy service and then map those values to sql query condition parameters.

Related

MariaDB Insert Stored Procedure With Parameter

I migrated my company database from MySQL to another hosting firm not knowing the hosting firm uses MariaDB, upon try to create my stored procedure with my IN parameter, MariaDB is seeing the parameter as a columnn. See the stored procedure code below with the error :
CREATE PROCEDURE ADD_WITHDRAWAL_A(IN withdrawalcode_p VARCHAR(25), IN id_p VARCHAR(8), IN amount_p VARCHAR(12), IN datewithdrawn_p VARCHAR(35), IN approved_p VARCHAR(8))
START TRANSACTION;
INSERT INTO Withdrawals(WithdrawalCode, IDD, Amount, DateWithdrawn, Approved)
VALUES (withdrawalcode_p, id_p, amount_p, datewithdrawn_p, approved_p);
UPDATE account SET AccountBalance = AccountBalance - amount_p WHERE IDD = id_p LIMIT 1;
COMMIT;
***** AFTER RUNNING THE ABOVE CODE, MARIADB GAVE THIS ERROR :
Error
SQL query:
INSERT INTO WithdrawalRequest( WithdrawalCode, IDD, Amount, DateWithdrawn, Approved )
VALUES (withdrawalcode_p, id_p, amount_p, datewithdrawn_p, approved_p);
MySQL said: Documentation
#1054 - Unknown column 'withdrawalcode_p' in 'field list'
The column name is WithdrawalCode and not 'withdrawalcode_p', 'withdrawalcode_p' is a parameter passed in to the stored procedure but the server is seeing it as a field name. I spoke with the hosting firm and they said their database is MariaDB and not MySQL. This same code worked in MySQL server.
I will appreciate any useful help rendered here.
You forgot to set non-default delimiters and to wrap the procedure body into BEGIN/END, which is necessary since it has more than one statement.
What happens in your case is that the procedure created with body START TRANSACTION, and the rest is considered to be a set of ordinary statements. Instead, try
DELIMITER $$
CREATE PROCEDURE ADD_WITHDRAWAL_A(IN withdrawalcode_p VARCHAR(25), IN id_p VARCHAR(8), IN amount_p VARCHAR(12), IN datewithdrawn_p VARCHAR(35), IN approved_p VARCHAR(8))
BEGIN
START TRANSACTION;
INSERT INTO Withdrawals(WithdrawalCode, IDD, Amount, DateWithdrawn, Approved)
VALUES (withdrawalcode_p, id_p, amount_p, datewithdrawn_p, approved_p);
UPDATE account SET AccountBalance = AccountBalance - amount_p WHERE IDD = id_p LIMIT 1;
COMMIT;
END $$
DELIMITER ;

Export whole query to a file using stored procedure

Need help on getting query on a text file using stored procedure in db2. I'm very new to this so please help.
Till now i am able to create a file and get get data in created file. But i want a complete query in the file not a single column data.
I have created a Stored Procedure like this
CREATE or replace PROCEDURE SCOPEMASTERP(my_statement varchar(10000))
AUTONOMOUS
BEGIN
DECLARE v_filehandle UTL_FILE.FILE_TYPE;
DECLARE v_dirAlias VARCHAR(50) DEFAULT 'mydir';
DECLARE v_filename VARCHAR(100) DEFAULT 'bluestar_transaction-';
DECLARE v_format VARCHAR(200);
SET v_format = '%s\n';
set v_filename = concat(v_filename, VARCHAR_FORMAT(current_date, 'DD-MM-YYYY'));
set v_filename = concat(v_filename, '.log');
CALL UTL_DIR.CREATE_OR_REPLACE_DIRECTORY('D:', '/archivelog/asd/');
SET v_filehandle = UTL_FILE.FOPEN(v_dirAlias,v_filename,'a');
CALL UTL_FILE.PUTF(v_filehandle,v_format, my_statement);
CALL DBMS_OUTPUT.PUT_LINE('Wrote to file: ' || v_filename);
CALL UTL_FILE.FCLOSE(v_filehandle);
END
and i have created a trigger inside trigger i am calling stored procedure
CREATE OR REPLACE TRIGGER SCOPEMASTER_Trigger
AFTER INSERT ON SERVEIT.SCOPENAMEMASTER
REFERENCING NEW AS N
FOR EACH ROW
BEGIN ATOMIC
call SCOPEMASTERP(N.SCOPENAMEID);
END
insert statement i am executing
Insert into SERVEIT.SCOPENAMEMASTER (SCOPENAMEID) values (1013)
GO
And file which is creating in "D" drive i am getting
But instead of just getting 1013 i need the complete query in the file
Insert into SERVEIT.SCOPENAMEMASTER (SCOPENAMEID) values (1013)
What changes i need to do please help. Thanks in advance!!
There are no special registers/variables/etc. available in DB2 that provide the SQL statement that is being executed, so what you're asking for is not possible.

Teradata : Stored Procedure : parameter column name

I'm looking for help with a stored procedure in teradata. I want to update a whole table and for this I'm trying to use a for loop cursor. the problem is that my update is defined via column names passing through parameters to the SP.
I've seen it can be possible to use dynamic sql to do that but I haven't found any information on the subject concerning for loop cursor and dynamic sql. Is it possible with FOR LOOP CURSOR ?
I've tried to do only the select and calculs with dynamic sql, it works fine but then the problem is to update the table from the cursor on the select. In this case how to update a table from my cursor?
I let you show my code.
loop cursor :
REPLACE PROCEDURE [database].calDELAI
(
IN dateDebut VARCHAR(30),
IN dateFin VARCHAR(30),
IN delay VARCHAR(30)
)
BEGIN
DECLARE DATE_DEBUT_COLONNE VARCHAR(64);
DECLARE DATE_FIN_COLONNE VARCHAR(64);
SET DATE_DEBUT_COLONNE=dateDebut;
SET DATE_FIN_COLONNE=dateFin;
FOR for_loop_update AS cur_select_set CURSOR FOR
SELECT
TMP.DATE_FIN_COLONNE-TMP.DATE_DEBUT_COLONNE
FROM [database].ORD_T_DETL_ORDR_DELAI AS TMP
/* the select is more complicated but here is the spirit of it.*/
DO
IF (delay='DELAI1') THEN SET DELAI1=NB_JR_OUVRABLE;
END IF;
END FOR ;
END ;
The errors given by teradata are :
SPL1027:E, Missing/Invalid SQL statement'E(3810):Column/Parameter '[database].TMP.DATE_FIN_COLONNE' does not exist.'.
SPL2001:E, Undefined symbol 'DELAI1'.
SPL2001:E, Undefined symbol 'NB_JR_OUVRABLE'.
Thanks in advance for your replies and your help.
The call statement should contain all the input Paramenters , make sure you are specifying all the input parameters correctly. Could you please provide your call statement.

Detecting and Using CURRENT SCHEMA in DB2 v8

I have a very big Stored Procedure in iSeries DB2 v8 which does the following:
Calls other stored procedures inside the same schema
Prepares dynamic sql statments from strings and runs them
Calls other functions from the same schema
Uses various tables from the same schema
My problem is that this Stored Procedure and the accompanying functions may change from that schema into another (ie. from 'superlib' to 'restorelib') and the whole code is currently hardcoded to run with the named schema.
What I want is to be able to do one of the two: either pass the name of the schema where everything is located via a parameter to the stored procedure, or have the stored procedure detect the name of the schema and use it to run itself.
This is a sample of my current code:
SELECT COUNT(*) INTO TotalNotDone FROM superlib.PROCESSTABLES WHERE PROCESS_FLAG < 1;
WHILE TotalNotDone > 0 DO
SET SQLLOOPSTMT = 'select name_to_proces from ' CONCAT SOURCELIBRARY CONCAT '.processtables where process_flag = 0' ;
PREPARE LOOPSTMT FROM SQLLOOPSTMT ;
OPEN LOOPCUR ;
FETCH LOOPCUR INTO TABLETOPROCESS ;
CALL superlib.SP_RESTORE_INSERTS ( SOURCELIBRARY , DESTLIBRARY , TABLETOPROCESS, P_STARTTIME ) ;
CLOSE LOOPCUR;
SELECT COUNT(*) INTO TotalNotDone FROM superlib.PROCESSTABLES WHERE PROCESS_FLAG < 1;
END WHILE ;
What I want is for NOT to have to write superlib inside the stored procedure to call or reference the tables i'm using and just have the Stored Procedure recognize it currently IS living in the schema superlib.
I've tried SET CURRENT SCHEMA = 'SUPERLIB'; and SET SCHEMA = 'SUPERLIB'; but neither works when calling the TABLES.
I've also changed the path when creating the Stored Procedure from:
SET PATH "QSYS","QSYS2","SYSPROC","SYSIBMADM","PROGUSER1" ;
to
SET PATH "QSYS","QSYS2","SYSPROC","SYSIBMADM","SUPERLIB" ;
but that apparently does nothing.
i believe you'll need to set the current path on the connection that calls the stored proc, not just when creating it.
see this: Weblogic: Call DB2 stored procedure without schema name (property currentSchema)
current path documentation here: http://publib.boulder.ibm.com/infocenter/db2luw/v8//topic/com.ibm.db2.udb.doc/admin/r0005877.htm

How can I call a DB2 stored procedure with OUT parameters from SQuirreL SQL?

I really like SQuirreL SQL as a SQL query tool, but I've never been able to get it to call stored procedures in our AS/400 DB2 database. I always get the error "The number of parameter values set or registered does not match the number of parameters." I've double-checked the number of params and had no luck. This is the syntax I've tried for a procedure that takes one IN and one OUT:
call SOMESPROC(12345, ?);
It seems that SQuirrel currently is not capable of doing that on AS/400 DB2.
Using the open source "SQL Workbench/J" (http://www.sql-workbench.net/) I was able to call a procedure:
wbcall SOMESPROC(12345, ?);
It has its own command for calling a procedure "wbcall". Use ? for out parameters.
Note: While installing SQL Workbench/J make sure to download the right DB2 driver from IBM and also add the licence file while adding the driver inside SQL Workbench/J.
In Squirrel you can use something like this. You'll want to make sure the type of the declared variable matches the type of your out parameter in the stored procedure.
BEGIN
DECLARE outParam INT;
STORED_PROC_NAME(outParam);
END
If you also need to provide input for the procedure you could do this.
BEGIN
DECLARE outParam INT;
STORED_PROC_NAME('input', outParam);
END
You also need to change the statement separator to something other than ;. Otherwise it will break up the statement and try to send each piece individually.
In the pro version of DbVisualizer, with the "Process Parameter Markers in SQL" under the SQL Commander menu option enabled, it will allow the "?" param
call SOMESPROC(12345, ?);
through trial and error, I was able to see the results in Squirrel.
create or replace variable var4 char(1);
create or replace variable var5 decimal(3,0);
create or replace variable var6 char(60);
call getthedata('XXX',123456789,'1234567',var4,var5,var6);
select var4,var5,var6 from sysibm.sysdummy1; -- displays OUT parms
I would think that if there is one in then the call should be:
CALL SomeSProc(12345)
to get a result maybe try:
SELECT * FROM SomeSProc(12345)
Here is an tested example which works on Squirrel 3.7 with a db2 stored procedure . The trick is to passe with an transitional stored procedure MY_PROC_TEST to call the real stored procedure PROC_TEST.
change statement separator in squirrel > session > session properties > SQL : #
DROP PROCEDURE MY_PROC_TEST()#
CREATE PROCEDURE MY_PROC_TEST()
RESULT SETS 1 -- out resultset (call product)
LANGUAGE SQL
BEGIN
DECLARE flag SMALLINT; -- out parameter
CALL MY_PROC('2015', flag);
END #
CALL MY_PROC_TEST()#
END #
Then you can call the sored procedure like this :
CALL MY_PROC_TEST()#
This will work in Squirrel if you change the delimiter (as specified above). However, to see what the variable is, you need to do the following...
In my example, I will set the delimiter to a tildy (~). Include after last "end", before "select". Code begins here...
begin
declare inoutParm numeric(2,0);
call spMyStoredProcedure(
1234567
, inoutParm
);
declare global temporary table session.myTempTbl
(MyResult char(1024) )
with replace ;
insert into session.myTempTbl
(myResult)
values(inoutParm) ;
end
~
select myResult from session.myTempTbl
Mic Keeley
as400(db2) SQL Developer
I was able to cobble together some amalgamation of all of the above answers and came up with this which worked for me. I'm using Squirrel SQL 2018 connecting to an IBM AS/400 DB2 database. I did have to declare a statement separator, I used "#".
BEGIN
DECLARE success CHAR(1); -- output parameters
DECLARE message CHAR(300);
SET success = ' ';
SET message = ' ';
CALL myProc('some', 'params', 4, success, message);
DECLARE GLOBAL TEMPORARY TABLE session.myTmp(s_res CHAR(1), m_res CHAR(300)) WITH REPLACE;
INSERT INTO session.myTmp(s_res, m_res) VALUES(success, message);
END
# -- <- statement separator needs to be set to something other than ";" in this case it's set to "#"
SELECT * FROM session.myTmp;
change statement separator in squirrel > session > session properties > SQL : '#'
BEGIN
DECLARE inOutParam varchar(200);
set inOutParam = 'a value';
STORED_PROC_NAME(outParam);
END;
#

Resources