postgresql 如何编写不返回任何内容的 postgres 存储过程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1343954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:49:29  来源:igfitidea点击:

How do I write a postgres stored procedure that doesn't return anything?

postgresqlstored-proceduresplpgsql

提问by ScArcher2

How do I write a simple stored procedure in postgres that doesn't return a value at all? Even with a void return type when I call the stored procedure I get a single row back.

如何在 postgres 中编写一个根本不返回值的简单存储过程?即使在调用存储过程时使用 void 返回类型,我也会返回单行。

CREATE FUNCTION somefunc(in_id bigint) RETURNS void AS $$
BEGIN
   DELETE from test_table where id = in_id;
END;
$$ LANGUAGE plpgsql;

采纳答案by Michael Krelin - hacker

It's not the function that returns value, it's the SELECTyou used to call it. If it doesn't return any rows, it doesn't run your function.

不是返回值的函数,而是SELECT您用来调用它的函数。如果它不返回任何行,则它不会运行您的函数。

回答by Michael Krelin - hacker

You can achieve "nothing returned" by abusing set-returning functions:

您可以通过滥用 set-returning 函数来实现“无返回”:

Simple function:

简单的功能:

create function x () returns setof record as $$
begin
return;
END;
$$ language plpgsql;

Now you can:

现在你可以:

# select x();
 x
---
(0 rows)

In case it doesn't work for you (sorry, I'm using 8.5), try with this approach:

如果它对您不起作用(抱歉,我使用的是 8.5),请尝试使用以下方法:

# create function x (OUT o1 bool, OUT o2 bool) returns setof record as $$
begin
return;
END;
$$ language plpgsql;
CREATE FUNCTION

The parameters are irrelevant, but:

参数无关紧要,但是:

  • You need > 1 of them
  • They have to be named
  • 你需要 > 1 个
  • 他们必须被命名

And now you can:

现在你可以:

# select * from x();
 o1 | o2
----+----
(0 rows)

回答by George Silva

You are doing just fine. You dont need to add anything else.

你做得很好。您无需添加任何其他内容。

The result of the row is null, so it is a void return.

该行的结果为空,因此它是一个空返回。

I don't think theres something you can do about that. Checking my void functions all of them are just like yours.

我认为你对此无能为力。检查我的 void 函数,它们都和你的一样。

returns void as $$ and no return statement in code block.

将 void 作为 $$ 返回,并且代码块中没有 return 语句。