函数是否可以返回一条记录,但如果结果为空,则返回零记录。例如:
如果我有一个不包含数据的空表......
CREATE TABLE Foo
(
FooID SERIAL CONSTRAINT PK_Foo PRIMARY KEY,
FooValue INTEGER NOT NULL
);
......和一个功能......
CREATE OR REPLACE FUNCTION GET_OneFoo()
RETURNS Foo
AS $$
SELECT
FooID,
FooValue
FROM
Foo
LIMIT 1
$$ LANGUAGE SQL;
......然后......
SELECT GET_OneFoo()
......导致......
Total query runtime: 11 ms.
1 row retrieved.
......但是......
SELECT
FooID,
FooValue
FROM
Foo
LIMIT 1
......导致......
Total query runtime: 10 ms.
0 rows retrieved.
答案 0 :(得分:3)
RETURNS setof Foo
而不是
RETURNS Foo
后者返回复合类型:
http://www.postgresql.org/docs/current/static/rowtypes.html
http://www.postgresql.org/docs/current/static/extend-type-system.html#AEN51678