我如何使用两个光标,一个基于另一个的输出?基本上我想要得到的是用以前的状态值替换所有等于“S”的状态。
- 光标 day_to_process:列出状态等于“S”的所有日期
- 光标 status_to_process :正在获取“S”之前的最后一个状态
我得到的错误是:
错误:缺少表“day_to_process”的 FROM 子句条目其中:PL/pgSQL 函数 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 ;
$$;