Subject use variable cursors in PSQL
Author chris.waldmann
In a stored procedure I use two cursors to compare two result set row by row.

I want to use the same stored procedure for different tables and alter only the table in the cursor.

Is there a way to alter the tabel name in a cursor?
Is there somthing like cursor variable or cursor reference?

Here some code of the solution for one table:

create procedure
....
declare CUR_OLD cursor for (
select STARTTIME, STOPTIME, PORTION
from SHDWTBLSTUDYFEEDING STSF
where STSF.UIDUPDATE = :UPDATE_OLD
order by STSF.STARTTIME);
declare CUR_NEW cursor for (
select STARTTIME, STOPTIME, PORTION
from SHDWTBLSTUDYFEEDING STSF
where STSF.UIDUPDATE = :UPDATE_NEW
order by STSF.STARTTIME);
...
begin
open CUR_OLD;
open CUR_NEW;

while (1 = 1)
do begin
...
/* fetch one slot from old settings */
fetch CUR_OLD
into VAR_START_OLD, VAR_STOP_OLD, VAR_PORTION_OLD;
VAR_ROW_COUNT_OLD = row_count;

/* fetch one slot from new settings */
fetch CUR_NEW
into VAR_START_NEW, VAR_STOP_NEW, VAR_PORTION_NEW;
VAR_ROW_COUNT_NEW = row_count;

/* compare old and new settings */
if ((VAR_START_OLD is distinct from VAR_START_NEW)
...
end
close CUR_OLD;
close CUR_NEW;
end