SQLCODE=-501, SQLSTATE=24501 with Cursor - db2

I have written one procedure. I am getting the below error.
Can someone help me with this.
Create procedure p1()
declare cur1 cursor WITH HOLD for STMT1;
DECLARE CONTINUE HANDLER FOR SQLSTATE '24501'
BEGIN
OPEN index_list ;
set exitcode = 1;
END ;
set TEXT =();
PREPARE STMT1 FROM TEXT;
OPEN cur1 ;
FETCH FROM cur1 INTO VAR2,
VAR1 ;
WHILE (SQLCODE = 0)
DO
EXECUTE IMMEDIATE 'SET PASSTHRU SAMPLE' ;
----
-----
EXECUTE IMMEDIATE 'SET PASSTHRU RESET' ;
FETCH FROM cur1 INTO VAR2,
VAR1 ;
END WHILE;
This is my code outline. My problem is After that 'SET PASSTHRU RESET', cursor is getting closed & the second fetch is failing with the below error.even I specified the curos a WITH HOLD option.
error:
The cursor specified in a FETCH statement or CLOSE statement is not open or a cursor variable in a cursor scalar function reference is not open.. SQLCODE=-501, SQLSTATE=24501, DRIVER=4.17.30
Run of routine failed.
can some one please let me know, what i have to add here.
I am new bee for db2 & never met with this scenario. That is why I am asking the experts.

Related

remove output pharentesis in a function

I'm trying to run this function and all records in the output have a parenthesis, how can I get rid of them?
the below function simply outputs the records in the loop for debugging, I just want to remove the pharentesis.
CREATE OR REPLACE FUNCTION public.loop_test(_report_id integer)
RETURNS void
LANGUAGE plpgsql
AS $function$
declare
_rid RECORD ;
BEGIN
FOR _rid IN
SELECT id FROM schema.table where id=_report_id
loop
raise notice '%',_rid;
END LOOP;
END
$function$
;
This is what I get (sample)
(1755)
(1789)
(1757)
(649)
(1781)
I need just the numbers..
Let me clarify that I would like to use the _rid variable on an insert within this function but until I don't get rid of the pharentesis I can't move on.

UPDATE/SET/OUTPUT/FROM with output into variable

Consider the following stored procedure
CREATE PROCEDURE AssignCodeToCustomer (#customerId int)
AS
BEGIN
DECLARE #code NVARCHAR(255)
BEGIN TRY
BEGIN TRANSACTION
SELECT #code = (
UPDATE
Codes
SET
CustomerId = #customerId
OUTPUT
INSERTED.Code
FROM (
SELECT TOP 1
Code
FROM
Codes
) AS c
WHERE
c.Code = Codes.Code
-- Other stuff
COMMIT TRANSACTION
END TRY
BEGIN CATCH
BEGIN
ROLLBACK TRANSACTION
EXEC spLogSQLError
END
END CATCH
END
GO
I get an error 'Incorrect syntax near the keyword UPDATE' on line 10 (which holds the keyword UPDATE). I could also first select a code and then assign it, but with concurrency in mind I want only one query. The query works if I don't try to set the output value into the variable. How can I fix this error or should I use another approach?

While loop in cursor not working properly

I tried storing the result in a varable and use coalece.
But it is not working.
do
$$
declare
v_deptno numeric := 10;
stored_empno numeric;
curempno cursor is select empno from emp e where e.deptno = v_deptno;
recempno record;
begin
open curempno;
fetch curempno
into recempno;
while found
loop
fetch curempno into recempno;
stored_empno:=recProdTest.empno;
raise info 'empno = %',coalesce(recProdTest.empno, stored_empno);
end loop;
close curempno;
end $$ language plpgsql;
Please help me.
Thanks.
Your program logic is wrong, and that won't work in Oracle either.
How many FETCH statements are executed before the first RAISE? Exactly, two: one before the loop, one in the loop.
So you are skipping the first result. The final NULL is because you don't check FOUND between FETCH and RAISE, so you report the empty result you get because the cursor is done.
Try a loop like this:
OPEN curempno;
LOOP
FETCH curempno INTO recempno;
EXIT WHEN NOT FOUND;
RAISE INFO 'empno = %', recempno.empno;
END LOOP;

Declaring the table name in constant in Postgres Stored procedure

I have a sample stored procedure where in I have to use a table for multiple operations. I want to declare the table name as a constant and then re-use it wherever required. Below is the sample code which i wrote:
CREATE OR REPLACE FUNCTION get_data()
RETURNS void AS
$func$
DECLARE
table_name_a CONSTANT TEXT = asp.monitoring_bookmark_original;
cursor_file CURSOR FOR
select distinct filename,systemuid from table_name_a;
cursor_data CURSOR FOR
select * from table_name_a where filename = v_filename and systemuid=v_systemuid order by mindatetime, maxdatetime;
BEGIN
--open the file cursor
//logic goes here
END;
$func$
LANGUAGE plpgsql;
When I try to run this procedure I am getting error:
ERROR: missing FROM-clause entry for table "asp"
LINE 1: SELECT asp.monitoring_bookmark_original
What is wrong in this code? How do I correct this?
Well you can use dynamic SQL, but realize dynamic SQL often adds way more complexity. Good when really needed but should be avoided when possible. The following shows what would be needed for what you want to do. Is not having to type the table name for each SQL statement worth the additional trouble?
create or replace function get_data()
returns void as
$func$
declare
table_name_a constant text = 'asp.monitoring_bookmark_original';
file_cursor text = 'select distinct filename,systemuid from %i';
file_ref refcursor;
file_rec record;
data_cursor text =$stmt$select * from %i where filename = '%s' and systemuid= '%s' order by mindatetime, maxdatetime$stmt$;
data_ref refcursor;
data_rec record;
begin
--open the file cursor
open file_ref for execute format(file_cursor,table_name_a);
loop
fetch next from file_ref into file_rec;
exit when not found;
-- and extending from what the second query inplies
open data_ref for execute format(data_cursor,table_name_a,file_rec.filename,file_rec.systemid);
loop
fetch next from data_ref into data_rec;
exit when not found;
--//logic goes here
end loop;
end loop ;
end;
$func$
language plpgsql;

SQLCODE=-501 SQLSTATE=24501 with Cursor

I have written a stored procedure as
CREATE OR REPLACE PROC1()
DECLARE VAR1 INT;
DECLARE VAR2 INT;
DECLARE TEXT VARCHAR(1000);
DECLARE exitcode INTEGER DEFAULT 0;
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE CUR1 CURSOR WITH HOLD for STMT1 ;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET exitcode = 1;
SET TEXT= (-----);
PREPARE STMT1 FROM TEXT;
open cur1;
fetch from cur1 into var1,var2;
while(sqlcode =0)
do
--
--
CALL SYSPROC.ADMIN_CMD('REORG TABLE emp1 ');
set exit code = 0;
fetch from cur1 into var1,var2;
IF (exitcode = 1)
THEN
LEAVE wloop;
END IF;
end while;
close cur1;
My question is even I declared my cursor as WITH HOLD option, after the first fetch the cursor is closing & throwing -501 error.If i remove the REORG statement from the loop. Then cursor is working normally,fetching all the rows. Can some one tel me the way to keep my cursor to be open even If i use the REORG statement inside the loop.
Thanks in advance