在 PostgreSQL 函数中使用变量

Jam*_*ton 2 postgresql variables function navicat

当我尝试运行自定义 PostgreSQL 函数时出现此错误:

\n\n
ERROR:  query has no destination for result data\n
Run Code Online (Sandbox Code Playgroud)\n\n

PostgreSQL 函数对我来说非常新。我正在使用 Navicat for PostgreSQL 11.0.17。

\n\n

我有一个名为 Translations 的表,其中包含三列:id、english、fran\xc3\xa7ais。以下是我在控制台窗口中创建函数的方法:

\n\n
test=# CREATE FUNCTION add_translation(english varchar(160), fran\xc3\xa7ais varchar(160))\n  RETURNS integer \n  AS $BODY$\nDECLARE\n  translation_id integer;\nBEGIN\n  INSERT INTO translations\n          ("english", "fran\xc3\xa7ais")\n   VALUES (english, fran\xc3\xa7ais)\nRETURNING id\n       AS translation_id;\n   RETURN translation_id;\nEND;\n$BODY$\nLANGUAGE plpgsql;\nQuery OK, 0 rows affected (0.02 sec)\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我从控制台窗口调用它时,我收到一条不太有用的错误消息。

\n\n
test=# add_translation(\'one\', \'un\');\nERROR:  syntax error at or near "add_translation"\nLINE 1: add_translation(\'one\', \'un\')\n        ^\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我从“设计功能”窗口调用它时,我收到顶部引用的错误。

\n\n

我特别想隔离translation_id,因为在这个函数的最终版本中,我想将翻译表中的最新id插入到不同表中的新记录中。

\n\n

我也尝试过:

\n\n
DECLARE\n  translation_id integer;\nBEGIN\n  INSERT INTO translations\n          ("english", "fran\xc3\xa7ais")\n   VALUES (english, fran\xc3\xa7ais);\n  SELECT LASTVAL() INTO translation_id;\n  RETURN translation_id;\nEND;\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我从“设计功能”面板运行它时,它的行为正确,但是当我从控制台调用它时,我得到了与以前相同的错误。

\n\n

如果您能推荐任何好的教程和示例来了解如何在 postgres 函数中正确使用变量,我将不胜感激。

\n

Clo*_*eto 6

从控制台你需要一个select命令

\n\n
select add_translation(\'one\', \'un\');\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者

\n\n
select * from add_translation(\'one\', \'un\');\n
Run Code Online (Sandbox Code Playgroud)\n\n

你的函数可以是普通的 SQL

\n\n
create or replace function add_translation (\n    english varchar(160), fran\xc3\xa7ais varchar(160)\n) returns integer as $body$\n    insert into translations ("english", "fran\xc3\xa7ais")\n    values (english, fran\xc3\xa7ais)\n    returning id as translation_id;\n$body$ language sql;\n
Run Code Online (Sandbox Code Playgroud)\n\n

在 plpgsql 中,setof某种类型必须从查询“返回”

\n\n
create or replace function add_translation (\n    english varchar(160), fran\xc3\xa7ais varchar(160)\n) returns setof integer as $body$\nbegin\n    return query\n    insert into translations ("english", "fran\xc3\xa7ais")\n    values (english, fran\xc3\xa7ais)\n    returning id as translation_id;\nend;\n$body$ language plpgsql;\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者要返回单个值,请在 CTE 内插入

\n\n
create or replace function add_translation (\n    english varchar(160), fran\xc3\xa7ais varchar(160)\n) returns integer as $body$\ndeclare\n  translation_id integer;\nbegin\n    with i as (\n        insert into translations ("english", "fran\xc3\xa7ais")\n        values (english, fran\xc3\xa7ais)\n        returning id\n    )\n    select id into translation_id from i;\n    return translation_id;\nend;\n$body$ language plpgsql;\n
Run Code Online (Sandbox Code Playgroud)\n