I want to run an UPSERT in a WHILE
loop . This is my query:
do $$ declare start_date date := '2021-01-16';
begin
while start_date::date < '2021-03-01' loop
insert
into
A ( id,
"year",
"month",
movement,
status)
(
select
id,
date_year,
date_month,
s_movement,
move_status
from
B
where
date_year = extract(year
from
start_date::date)
and date_month = (extract(month
from
start_date::date)+ 1)
and date_day = extract(day
from
start_date::date))
on conflict (id) do
update set status = move_status, movement = s_movement;
start_date:=start_date+interval '1 day';
end loop;
end $$
But when I run this query, it gives error:
SQL Error [42703]: ERROR: column "move_status" does not exist Hint: There is a column named "move_status" in table "*SELECT*", but it cannot be referenced from this part of the query.
How to fix it?