5
Generally, you can expand well known row types (a.k.a. record type, complex type, composite type) with the simple syntax @Daniel supplied:
通常,您可以使用提供的简单语法@Daniel扩展众所周知的行类型(a.k.a。记录类型,复杂类型,复合类型):
SELECT i.name, (compute_prices(i.id, current_date)).*
FROM items i
WHERE i.type = 404;
However, if your description is accurate ...
但是,如果您的描述准确无误......
The compute_prices sp returns a setof record.
compute_prices sp返回一组记录。
... we are dealing with anonymous records. Postgres does not know how to expand anonymous records and throws an EXCEPTION in despair:
......我们正在处理匿名记录。 Postgres不知道如何扩展匿名记录并在绝望中抛出一个EXCEPTION:
ERROR: a column definition list is required for functions returning "record"
PostgreSQL 9.3
There is a solution for that in Postgres 9.3. LATERAL
, as mentioned by @a_horse in the comments:
Postgres 9.3中有一个解决方案。正如@a_horse在评论中提到的那样:LATERAL:
SELECT i.name, sp.*
FROM items i
LEFT JOIN LATERAL compute_prices(i.id,current_date) AS sp (
price numeric(15,2)
,discount numeric(5,2)
,taxes numeric(5,2)
) ON TRUE
WHERE i.type = 404;
Details in the manual.
手册中的详细信息。
PostgreSQL 9.2 and earlier
Things get hairy. Here's a workaround: write a wrapper function that converts your anonymous records into a well known type:
事情变得多毛了。这是一个解决方法:编写一个包装函数,将您的匿名记录转换为众所周知的类型:
CREATE OR REPLACE FUNCTION compute_prices_wrapper(int, date)
RETURNS TABLE (
price numeric(15,2)
,discount numeric(5,2)
,taxes numeric(5,2)
) AS
$func$
SELECT * FROM compute_prices($1, $2)
AS t(price numeric(15,2)
,discount numeric(5,2)
,taxes numeric(5,2));
$func$ LANGUAGE sql;
Then you can use the simple solution by @Daniel and just drop in the wrapper function:
然后你可以使用@Daniel的简单解决方案,只需输入包装函数:
SELECT i.name, (compute_prices_wrapper(i.id, current_date)).*
FROM items i
WHERE i.type = 404;
PostgreSQL 8.3 and earlier
PostgreSQL 8.3 has just reached EOL and is unsupported as of now (Feb. 2013).
So you'd better upgrade if at all possible. But if you can't:
PostgreSQL 8.3刚刚达到EOL,截至目前(2013年2月)尚未得到支持。因此,如果可能的话,你最好升级。但如果你不能:
CREATE OR REPLACE FUNCTION compute_prices_wrapper(int, date
,OUT price numeric(15,2)
,OUT discount numeric(5,2)
,OUT taxes numeric(5,2))
RETURNS SETOF record AS
$func$
SELECT * FROM compute_prices($1, $2)
AS t(price numeric(15,2)
,discount numeric(5,2)
,taxes numeric(5,2));
$func$ LANGUAGE sql;
Works in later versions, too.
也适用于更高版本。
The proper solution would be to fix your function compute_prices()
to return a well know type to begin with. Functions returning SETOF record
are generally a PITA. I only poke those with a five-meter-pole.
正确的解决方案是修复函数compute_prices()以返回一个众所周知的类型。返回SETOF记录的函数通常是PITA。我只戳那些五米高的杆子。