输出多列的 PostgreSQL 函数或存储过程?
这就是我理想中想要的。想象一下,我有一个包含行 A 的表。
我想做的是:
SELECT A, func(A) FROM table
输出有 4 列。
有什么办法可以做到这一点吗?我见过关于自定义类型或其他任何可以让你得到看起来像的结果的东西
A,(B,C,D)
但是,如果我能让一个函数返回多列而无需再进行任何欺骗,那就太好了。
有什么东西可以做这样的事情吗?
Here is what I ideally want. Imagine that I have a table with the row A.
I want to do:
SELECT A, func(A) FROM table
and for the output to have say 4 columns.
Is there any way to do this? I have seen things on custom types or whatever that let you sort of get a result that would look like
A,(B,C,D)
But it would be really great if I could have that one function return multiple columns without any more finagling.
Is there anything that can do something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果函数 func 仅返回包含 3 个值的 1 行,例如:
然后执行
SELECT a, func(a) FROM table1
您将得到:但是,如果你执行:
你会得到:
或者,使用 CTE (公共表表达式),如果执行:您
还将得到:
注意:您还可以使用以下查询来获得相同的结果:
或
If the function func returns only 1 row with 3 values, such as:
and you then execute
SELECT a, func(a) FROM table1
you'll get:but, if you execute:
you'll get:
or, using CTE (Common Table Expressions), if you execute:
you'll also get:
Note: you may also use the following queries to obtain the same results:
or
我同意 bambam 的答案,但想指出 JackPDouglas 更简洁的语法
SELECT a, (func(a)).* FROM table1
,从我的测试来看,实际上会为每列执行一次该函数返回,而 CTE 表达式只会执行该函数一次。因此,如果函数执行时间较长,则首选 CTE 表达式。I agree with bambam's answer but would like to point out that JackPDouglas's more succinct syntax
SELECT a, (func(a)).* FROM table1
, from my tests, would actually execute the function once for each column returned whereas the CTE expression will only execute the function once. So the CTE expression is preferred if the function takes a long time to execute.如果函数始终返回 3 列,您可以执行以下操作:
如果您可以从视图中访问表,也许您可以以某种方式创建视图
,那么它只是一个 SELECT * FROM v 。但这看起来又可以使用继承。
If the function always returns 3 columns, you can do something like that:
If you can access the table from within a view, maybe you can create a view in some way
then it's just a
SELECT * FROM v
. But again this looks like Inheritance could be used.我想你会想返回一条包含多列的记录?在这种情况下,您可以使用返回类型
RECORD
例如。这将允许您返回一个具有任意数量列的匿名变量。您可以在此处找到有关所有不同变量的更多信息:http://www .postgresql.org/docs/9.0/static/plpgsql-declarations.html
关于返回类型:
http://www.postgresql.org/docs/9.0/static/xfunc-sql.html#XFUNC-OUTPUT-PARAMETERS
如果要返回具有多个列的多个记录,首先检查是否必须为此使用存储过程。可以选择仅使用
VIEW
(并使用 WHERE 子句查询)。如果这不是一个好的选择,则可以从版本 9.0 中的存储过程返回TABLE
。I think you will want to return a single record, with multiple columns? In that case you can use the return-type
RECORD
for example. This will allow you to return an anonymous variable with as many columns as you want. You can find more information about all the different variables here:http://www.postgresql.org/docs/9.0/static/plpgsql-declarations.html
And about return types:
http://www.postgresql.org/docs/9.0/static/xfunc-sql.html#XFUNC-OUTPUT-PARAMETERS
If you want to return multiple records with multiple columns, first check and see if you have to use a stored procedure for this. It might be an option to just use a
VIEW
(and query it with a WHERE-clause) instead. If that's not a good option, there is the possibility of returning aTABLE
from a stored procedure in version 9.0.