"ERROR: too few arguments for format()" in a PL/pgSQL function
"ERROR: too few arguments for format()" in a PL/pgSQL function
我创建了一个函数来从使用不同时间段的多个表中提取信息:
CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF tmpindicadores AS
$BODY$
DECLARE
r tmpindicadores%rowtype;
record tmpindicadores.relname%type;
tmpdata_inicio TIMESTAMP;
tmpdata_fim TIMESTAMP;
dia_inicio INTEGER := 01;
dia_fim INTEGER := 01;
mes INTEGER;
ano INTEGER;
BEGIN
SELECT EXTRACT (MONTH FROM CURRENT_DATE) INTO mes;enter code here
SELECT EXTRACT (YEAR FROM CURRENT_DATE) INTO ano;
tmpdata_inicio := ano || '-' ||mes ||'-' ||dia_inicio;
tmpdata_fim := ano || '-' ||mes + 1 ||'-' ||dia_fim;
FOR r IN SELECT relname FROM tmpindicadores
LOOP
record = r.relname;
EXECUTE format($$INSERT INTO indicadores1 (SELECT * FROM %I WHERE time_date >= %tmpdata_inicio
AND time_date <= %tmpdata_fim)$$,record);
RETURN NEXT r;
END LOOP;
COPY indicadores1 TO '/var/app_data/indicadores/arquivos/indicadores1.csv' DELIMITER ';' CSV
HEADER;
RETURN;
END
$BODY$
LANGUAGE plpgsql;
SELECT * FROM getAllFoo();
但我得到:
ERROR: too few arguments for format.
此错误发生在 EXECUTE
行。当我将变量 tmpdata_inicio
和 tmpdata_fim
替换为“2022-04-01 00:00:00”和“2022-04-05 00:00:00”时,它工作得很好。
我做错了什么?
您为 format()
使用了不正确的格式说明符。
这可行:
EXECUTE format($$INSERT INTO indicadores1 SELECT * FROM %I WHERE time_date >= %L AND time_date <= %L$$, record, tmpdata_inicio, tmpdata_fim);
而是使用 USING
子句传递 values:
EXECUTE format($$INSERT INTO indicadores1 SELECT * FROM %I WHERE time_date >= AND time_date <= $$, record)
USING tmpdata_inicio, tmpdata_fim;
也就是说,我会重写整个函数以清理和优化...
我创建了一个函数来从使用不同时间段的多个表中提取信息:
CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF tmpindicadores AS
$BODY$
DECLARE
r tmpindicadores%rowtype;
record tmpindicadores.relname%type;
tmpdata_inicio TIMESTAMP;
tmpdata_fim TIMESTAMP;
dia_inicio INTEGER := 01;
dia_fim INTEGER := 01;
mes INTEGER;
ano INTEGER;
BEGIN
SELECT EXTRACT (MONTH FROM CURRENT_DATE) INTO mes;enter code here
SELECT EXTRACT (YEAR FROM CURRENT_DATE) INTO ano;
tmpdata_inicio := ano || '-' ||mes ||'-' ||dia_inicio;
tmpdata_fim := ano || '-' ||mes + 1 ||'-' ||dia_fim;
FOR r IN SELECT relname FROM tmpindicadores
LOOP
record = r.relname;
EXECUTE format($$INSERT INTO indicadores1 (SELECT * FROM %I WHERE time_date >= %tmpdata_inicio
AND time_date <= %tmpdata_fim)$$,record);
RETURN NEXT r;
END LOOP;
COPY indicadores1 TO '/var/app_data/indicadores/arquivos/indicadores1.csv' DELIMITER ';' CSV
HEADER;
RETURN;
END
$BODY$
LANGUAGE plpgsql;
SELECT * FROM getAllFoo();
但我得到:
ERROR: too few arguments for format.
此错误发生在 EXECUTE
行。当我将变量 tmpdata_inicio
和 tmpdata_fim
替换为“2022-04-01 00:00:00”和“2022-04-05 00:00:00”时,它工作得很好。
我做错了什么?
您为 format()
使用了不正确的格式说明符。
这可行:
EXECUTE format($$INSERT INTO indicadores1 SELECT * FROM %I WHERE time_date >= %L AND time_date <= %L$$, record, tmpdata_inicio, tmpdata_fim);
而是使用 USING
子句传递 values:
EXECUTE format($$INSERT INTO indicadores1 SELECT * FROM %I WHERE time_date >= AND time_date <= $$, record)
USING tmpdata_inicio, tmpdata_fim;
也就是说,我会重写整个函数以清理和优化...