DB2 stored procedure in sql using squirrel. Unable to loop - sql

I am unable to use %notfound as I get the following error. How can I get past this?? I'm unable to figure out a way to use cursors apart from this method. Please help with other methods to use a cursor loop without using %notfound.
The character "%" following "EXIT WHEN c_rqstid" is not valid.. SQLCODE=-7, SQLSTATE=42601, DRIVER=3.63.123 SQL Code: -7, SQL State: 42601
I have set # as the delimeter and the code is as follows
create PROCEDURE TEST111()
AS:
begin
DECLARE c_id integer;
DECLARE c_isactive integer;
DECLARE c_status integer;
CURSOR c_rqstid is SELECT REQUESTID,REQUESTSTATUS,ISACTIVE FROM SAMPLE.REQUEST;
OPEN c_rqstid;
FOR LOOP FETCH c_rqstid into c_id,c_status,c_isactive ;
----will code this later
EXIT WHEN c_rqstid%NOTFOUND;
END LOOP;
CLOSE c_rqstid;
end
#
Thanks for any help in advance...

You'll need to check the SQLCODE or SQLSTATE variables:
http://pic.dhe.ibm.com/infocenter/db2luw/v10r1/topic/com.ibm.db2.luw.apdv.sqlpl.doc/doc/c0009028.html
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
WHILE(SQLSTATE = '00000') DO
SET p_sum = p_sum + p_sal;
FETCH FROM c INTO p_sal;
END WHILE;

Related

How to rectify Error #1064 in MariaDB procedure?

DELIMITER $$
CREATE PROCEDURE insert_priority_rows()
BEGIN
DECLARE max_heuristics INT DEFAULT 10;
DECLARE heuristic_ID INT;
DECLARE cur CURSOR FOR
SELECT heuristicID
FROM heuristics;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET heuristic_ID = 0;
DECLARE #study_ID INT;
SELECT MAX(studyID) INTO #study_ID FROM study;
OPEN cur;
REPEAT
FETCH cur INTO heuristic_ID;
IF heuristic_ID = 0 THEN
LEAVE;
END IF;
INSERT INTO heuristic_priority (studyID, heuristicID, h_priority)
VALUES (#study_ID, heuristic_ID, 'm');
UNTIL heuristicID = 0 END REPEAT;
CLOSE cur;
END$$
DELIMITER ;
It throws up an error as follows:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near '#study_ID INT;
What should I do to rectify this issue?
You don't need most of that code. The loop can be replaced with this query:
DELIMITER $$
CREATE PROCEDURE insert_priority_rows()
BEGIN
INSERT INTO heuristic_priority (studyID, heuristicID, h_priority)
SELECT m.max_study_id, h.heuristicID, 'm'
FROM heuristics AS h
CROSS JOIN (SELECT MAX(studyID) as max_study_id FROM study) AS m;
END$$

Two cursors sequence in procedure

I have the following problem with a procedure:
CREATE PROCEDURE TEST()
LANGUAGE SQL
BEGIN
DECLARE V_TEST VARCHAR(100);
DECLARE V_TEST_EXT VARCHAR(100);
DECLARE SQLCODE INTEGER DEFAULT -1;
DECLARE RET_CODE INTEGER DEFAULT -2;
DECLARE LIST_CMD VARCHAR(512);
DECLARE LIST_CMD_EXT VARCHAR(512);
DECLARE CUR_TEST CURSOR WITH RETURN FOR LIST_STMT;
DECLARE CUR_TEST_EXT CURSOR WITH RETURN FOR LIST_EXT_STMT;
DECLARE CONTINUE HANDLER FOR SQLEXECPTION SET RET_CODE = SQLCODE;
SET LIST_CMD = 'SELECT TEST FROM TESTTAB';
PREPARE LIST_STMT FROM LIST_CMD;
OPEN CUR_TEST;
FETCH CUR_TEST INTO V_TEST;
WHILE (RET_CODE <> 100) DO
FETCH CUR_TEST INTO V_TEST;
END WHILE;
CLOSE CUR_TEST;
SET LIST_CMD_EXT = 'SELECT TEST FROM TESTTAB';
PREPARE LIST_STMT_EXT FROM LIST_CMD_EXT;
OPEN CUR_TEST_EXT;
FETCH CUR_TEST_EXT INTO V_TEST_EXT;
WHILE (RET_CODE <> 100) DO
FETCH CUR_TEST_EXT INTO V_TEST_EXT;
END WHILE;
CLOSE CUR_TEST_EXT;
END;
The problem I am having is that the procedure does the first cursor loop but ignores the second one. I tried adding BEGIN and END to each loop but that didn't help as well.
I am running DB2 v10.5 Windows. I need this to work as a procedure. Is there any way to make the procedure run both loops?
Thank you for your help.-
You don't reset the RET_CODE variable before processing the 2-nd cursor.
Use something like this:
SET RET_CODE = 0;
OPEN CUR_TEST_EXT;
...
BTW, don't declare a cursor using 'WITH RETURN' clause if you process this cursor inside the procedure.

DB2 SQL Error: SQLCODE=-952, > SQLSTATE=57014, from Tibco BW

I am calling a DB2 stored proc from BW call procedure pallet.
when i test this proc in database ,it is getting executed properly.
But when i call through BW it throws exception
JDBC error reported: (SQLState = 57014) -
com.ibm.db2.jcc.am.SqlException: DB2 SQL Error: SQLCODE=-952,
SQLSTATE=57014, SQLERRMC=null,
My stored proc's code is as below
CREATE OR REPLACE PROCEDURE TABLE1_PURGE_PROC_V1 (IN v_REF_VERSION VARCHAR(3), OUT o_DELTETE_STATUS VARCHAR(7))
P1: BEGIN
--DECLARE v_TABLE_NAME VARCHAR(30);
DECLARE v_WHERE_CONDITION VARCHAR(1024);
DECLARE V_COUNT_QUERY VARCHAR(1024);
DECLARE v_COMMIT_COUNT INTEGER;
SET v_WHERE_CONDITION='REF_VERSION ='||v_REF_VERSION;
SET v_COMMIT_COUNT=10000;
CALL SCHEMA.DELETE_WITH_COMMIT_COUNT('SCHEMA.TABLE1',v_COMMIT_COUNT,v_WHERE_CONDITION);
CALL SCHEMA.DELETE_WITH_COMMIT_COUNT('SCHEMA.TABLE2',v_COMMIT_COUNT,v_WHERE_CONDITION);
CALL SCHEMA.DELETE_WITH_COMMIT_COUNT('SCHEMA.TABLE3',v_COMMIT_COUNT,v_WHERE_CONDITION);
CALL SCHEMA.DELETE_WITH_COMMIT_COUNT('SCHEMA.TABLE4',v_COMMIT_COUNT,v_WHERE_CONDITION);
INSERT INTO SCHEMA.DEBUG_LOG(PROC_NAME,LOG_TIME,MESSAGE) VALUES('TABLE1_PURGE_PROC_V1',CURRENT_TIMESTAMP,'ALL TABLE1 RELATED TABLES INVALID DATA DELETED FOR VERSION-'||v_REF_VERSION);
SET o_DELTETE_STATUS ='SUCCESS';
END P1
####################################### PROC 2 ###########################################################
CREATE OR REPLACE PROCEDURE DELETE_WITH_COMMIT_COUNT(IN v_TABLE_NAME VARCHAR(24), IN v_COMMIT_COUNT INTEGER, IN v_WHERE_CONDITION VARCHAR(1024))
NOT DETERMINISTIC
LANGUAGE SQL
P1: BEGIN
-- DECLARE Statements
DECLARE SQLCODE INTEGER;
DECLARE v_DELETE_QUERY VARCHAR(1024);
DECLARE v_DELETE_STATEMENT STATEMENT;
SET v_DELETE_QUERY = 'DELETE FROM (SELECT 1 FROM ' || v_TABLE_NAME || ' WHERE ' || v_WHERE_CONDITION
|| ' FETCH FIRST ' || RTRIM(CHAR(v_COMMIT_COUNT)) || ' ROWS ONLY) AS DELETE_TABLE';
PREPARE v_DELETE_STATEMENT FROM v_DELETE_QUERY;
DEL_LOOP:
LOOP
EXECUTE v_DELETE_STATEMENT;
IF SQLCODE = 100 THEN
INSERT INTO TEP.DEBUG_LOG(PROC_NAME,LOG_TIME,MESSAGE) VALUES('DELETE_WITH_COMMIT_COUNT',CURRENT_TIMESTAMP,'ALL DATA DELETED FROM'||v_TABLE_NAME||'QUERY USED IS'||v_DELETE_QUERY);
LEAVE DEL_LOOP;
END IF;
COMMIT;
END LOOP;
COMMIT;
END P1
As I researched ,it is being said that it is the interruption code in DB2.
How to handle this in DB2
I added Tibco timeout to 6 minutes its working now.
deleting 2 million data from 12 tables
Thanks guys for helping

Use the value of a string variable in SQL FOR-IN (SELECT) loop

I have an SQL procedure, that should execute a FOR-IN (SELECT ..) loop, where the SELECT content should vary depending on some input parameters. My idea was to store the SELECT string into a variable, and then try to extract the variable value in the FOR-IN loop but without success so far (earlier there was a fix SELECT statement used there, that's what I am trying to replace now). The code looks about as follows
PROCEDURE run(p_boolean BOOLEAN)
IS
BEGIN
DECLARE
v_mystring VARCHAR(50);
BEGIN
IF p_boolean = TRUE
THEN
v_mystring := 'SELECT something...';
ELSE
v_mystring := 'SELECT something else...';
END IF;
FOR p_table_name IN (would-like-to-use-the-value-of-v_mystring-here-some-way)
LOOP
...
END LOOP;
END;
END;
Being quite novice in SQL, it might well happen that the entire concept of trying to use a string variable value here is wrong. I browsed through some tutorials and tried some other ideas (e.g. cursor), but no result. Any idea is appreciated
Assuming Oracle's PL/SQL ,You can open a REFCURSOR using dynamic String and call it in a LOOP..
PROCEDURE run(p_boolean BOOLEAN)
IS
BEGIN
DECLARE
v_mystring VARCHAR(50);
v_my_ref_cursor sys_refcursor;
BEGIN
IF p_boolean = TRUE
THEN
v_mystring := 'SELECT something...';
ELSE
v_mystring := 'SELECT something else...';
END IF;
OPEN v_my_ref_cursor FOR v_mystring;
LOOP
FETCH v_my_ref_cursor INTO your_variables/record
EXIT WHEN v_my_ref_cursor%NOTFOUND;
..
END LOOP;
CLOSE v_my_ref_cursor;
END;
END;

looping inside stored procedures

I am writing a trigger in DB2. Inside that i wanted to use a loop.
This is to iterate through a set of values returned from a select statement.
How can this be done?
Anybody please share a tutorial or link which explains this.
Also is it ok to have nested loop?
You can use a cursor in your trigger:
http://en.wikipedia.org/wiki/Cursor_%28databases%29
A simple cursor example looks like this:
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE p_sum INTEGER;
SET p_sum = 0;
DECLARE p_sal INTEGER;
DECLARE c CURSOR FOR SELECT SALARY FROM EMPLOYEE;
OPEN c;
FETCH FROM c INTO p_sal;
WHILE(SQLSTATE = '00000') DO
SET p_sum = p_sum + p_sal;
FETCH FROM c INTO p_sal;
END WHILE;
CLOSE c;