How can i use two cursor , one based on the output of the other ? basically what i'm trying to get is replace all the status that equal 'S' with the previous values of status.
- Cursor day_to_process: is listing all the date where the status equal 'S'
- Cursor status_to_process : is getting the last status before 'S'
The error i'm getting is:
ERROR: missing FROM-clause entry for table "day_to_process" Where: PL/pgSQL function scrat.update_status()
create or replace function scrat.update_status() returns void
language plpgsql
as
$$
DECLARE
day_to_process CURSOR FOR (SELECT distinct inst_status.status_date
FROM scrat.inst_status
WHERE inst_status.status = 'S'
ORDER BY 1);
status_to_process CURSOR for (select inst_status.status, max(inst_status.status_date)
FROM scrat.inst_status
where inst_status.status <> 'S'
and inst_status.status_date < day_to_process.status_date
group by status
order by 2 desc
limit 1);
curr_date TEXT;
curr_status TEXT;
BEGIN
OPEN day_to_process;
OPEN status_to_process;
LOOP
FETCH day_to_process INTO curr_date;
FETCH status_to_process INTO curr_status;
update scrat.inst_status
set inst_status.status = status_to_process.status
where inst_status.status_date = curr_date;
END LOOP;
END ;
$$;