【发布时间】:2019-01-02 13:41:43
【问题描述】:
我正在创建一个 plpgsql 函数来填充查找表以获取日期信息。我的循环语句只执行第一个日期,而不是循环其他天数。
DECLARE
startDate ALIAS FOR $1;
endDate ALIAS FOR $2;
currentDate date;
dateDate integer;
yearDate integer;
monthDate integer;
dayDate integer;
weekDate integer;
dayofWeekDate integer;
weekofYearDate integer;
quarterDate integer;
BEGIN
DELETE FROM date_data;
FOR currentDate IN (SELECT * FROM generate_series(startDate::date,endDate::date,'1 day') s(currentDate)) LOOP
yearDate := (SELECT date_part('year', currentDate));
monthDate := (SELECT date_part('month', currentDate));
dayDate := (SELECT date_part('day', currentDate));
weekDate := (SELECT date_part('week', currentDate));
dayofWeekDate := (SELECT date_part('dow', currentDate));
quarterDate := (SELECT date_part('quarter', currentDate));
weekofYearDate := (SELECT date_part('week', currentDate));
dateDate := to_char(currentDate,'YYYYMMDD');
INSERT INTO date_data VALUES ( dateDate, yearDate, monthDate, dayDate, FALSE, dayofWeekDate, FALSE, NULL, FALSE, NULL, weekofYearDate, quarterDate);
RETURN dateDate;
END LOOP;
END;
我希望它循环插入预期值的时间序列,但它只是插入第一个日期而不是继续。
我正在使用 SELECT add_date_data2('2018-01-01','2019-01-01'); 调用该函数
谢谢。
【问题讨论】:
-
因为你在循环体中返回。
-
@stickybit 谢谢。搬家确实解决了我的问题。
标签: postgresql plpgsql