“错误:格式的论点太少()在PL/PGSQL功能中
我创建了一个函数,可以使用不同的时间段从几个表中提取信息:
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();
但是我得到:
错误:格式的参数太少。
此错误发生在执行
行中。当我替换变量时完美。
我在做什么错?
I created a function to extract information from several tables using different time period:
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();
But I get:
ERROR: too few arguments for format.
This error occurs in the EXECUTE
line. When I replace the variables tmpdata_inicio
and tmpdata_fim
with '2022-04-01 00:00:00' and '2022-04-05 00:00:00' it works perfectly.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
格式( )
。这将有效:
但是,使用子句:将 values 传递:
也就是说,我会重写整个函数以消毒和优化...
You are using incorrect format specifiers for
format()
.This would work:
But rather, pass values with the
USING
clause:That said, I would rewrite the whole function to sanitize and optimize ...