什么是 "non-SETOF function"?

What is a "non-SETOF function"?

进入PL/pgSQL…

我还在学习语法。有时,我会 运行 进入此错误消息:

ERROR: cannot use RETURN QUERY in a non-SETOF function

这听起来有点神秘,我在 Postgres 文档中找不到信息。因此问题:

同样,假设有这样的东西,什么是 SETOF 函数?

What's a non-SETOF function?

它是一个 returning 单个(标量)值的函数,例如integervarchar,例如

select upper('foo')` 

upper() 是一个 "non-SETOF" 函数,它只有 return 一个值。所以一个功能是例如定义为 returns integer 不能 return 一个 SELECT 查询的完整结果,它需要使用 return 42; 但不能使用 return query ...;


what's a SETOF function?

它是一个函数,return 是一个结果集 - 类似于 table(它通常声明为 returns table (...)。您可以像 table 一样使用它:

select *
from some_set_returning_function();

正如 documentation 所说:

an SQL function can be declared to return a set (that is, multiple rows) by specifying the function's return type as SETOF sometype, or equivalently by declaring it as RETURNS TABLE(columns). In this case all rows of the last query's result are returned.

例如,如果它 returns 一行或一个标量,它将是一个非 SETOF。