Postgresql for loop

Beaver Bridge - Jul 21 '23 - - Dev Community

Postgres에서 날짜 목록을 생성한 후 테이블에 하나씩 넣기 위해 loop를 돌리려면 이렇게 하면 된다.

do $$
declare
  dates date;
begin
  for dates in SELECT generate_series('2023-07-01'::DATE, '2023-07-31'::DATE, '1 day'::interval)::DATE loop
    insert into my_table(user_id, date, type) values ('my_user_id',dates, 'OFF');
  end loop;
end;
$$;
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player