postgresql 自定义函数 循环打印日期
CREATE OR REPLACE FUNCTION db.test() RETURNS text LANGUAGE plpgsql NOT FENCED NOT SHIPPABLE AS $$ DECLARE --declare data_array VARCHAR2(19)[]; field_tmp varchar2(19); month_numbers number(10); return_values text; i number(2); start_month timestamp; end_month varchar(10); BEGIN i :=0; start_month := timestamp'2020-07-01'; end_month := '2020-07-31';
#存在日期bug -- select (select regexp_split_to_array(age(quote_literal(end_month),start_month),' '))[1] into month_numbers;
#替换方案 select extract(day from (quote_literal(end_month)-start_month)) into month_numbers; LOOP select date_trunc('day', date'2020-07-01')+i into data_array[i]; --return_values:=return_values||i; EXIT WHEN i >=month_numbers; i := i + 1; END LOOP; foreach field_tmp in array data_array LOOP return_values := return_values || '__' || field_tmp; end loop; return return_values; END$$ /