gpt4 book ai didi

postgresql - PL/pgSQL EXECUTE语句中没有参数$1

转载 作者:行者123 更新时间:2023-11-29 11:38:27 25 4
gpt4 key购买 nike

我无法解决这个问题:

CREATE OR REPLACE FUNCTION dpol_insert(
dpol_cia integer, dpol_tipol character, dpol_nupol integer,
dpol_conse integer,dpol_date timestamp)
RETURNS integer AS
$BODY$
DECLARE tabla text := 'dpol'||EXTRACT (YEAR FROM $5::timestamp);
BEGIN
EXECUTE '
INSERT INTO '|| quote_ident(tabla) ||'
(dpol_cia, dpol_tipol, dpol_nupol, dpol_conse, dpol_date) VALUES ($1,$2,$3,$4,$5)
';
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

尝试时

SELECT dpol_insert(1,'X',123456,1,'09/10/2013')

返回下一条消息:

ERROR:  there is no parameter $1
LINE 3: ...tipol, dpol_nupol, dpol_conse, dpol_date) VALUES ($1,$2,$3,$...
^
QUERY:
INSERT INTO dpol2013
(dpol_cia, dpol_tipol, dpol_nupol, dpol_conse, dpol_date) VALUES ($1,$2,$3,$4,$5)

CONTEXT: PL/pgSQL function "dpol_insert" line 4 at EXECUTE statement

*** 错误 strong>***

ERROR: there is no parameter $1
SQL state: 42P02
Context: PL/pgSQL function "dpol_insert" line 4 at EXECUTE statement

最佳答案

这里有几个问题。眼前的问题是:

ERROR: there is no parameter $1

发生这种情况是因为您要交给 EXECUTE 的 SQL 中的 $1 与主函数体内的 $1 不同。 EXECUTE SQL 中的编号占位符在 EXECUTE 的上下文中,而不是在函数的上下文中,因此您需要为这些占位符提供一些 EXECUTE 参数:

execute '...' using dpol_cia, dpol_tipol, dpol_nupol, dpol_conse, dpol_date;
-- ^^^^^

参见 Executing Dynamic Commands在手册中了解详细信息。

下一个问题是您没有从函数返回任何RETURNS integer 的东西。我不知道您打算返回什么,但也许您的 tablea 有一个您想返回的 SERIAL id。如果是这样,那么您想要更像这样的东西:

declare
tabla text := 'dpol' || extract(year from $5::timestamp);
id integer;
begin
execute 'insert into ... values ($1, ...) returning id' into id using dpol_cia, ...;
-- ^^^^^^^^^^^^ ^^^^^^^
return id;
end

在你的函数中。

关于postgresql - PL/pgSQL EXECUTE语句中没有参数$1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19302098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com