作为查询来源表¶
函数在SQL语句的FROM子句中作为range table entry出现。
在这种情况下,只有函数返回值会返回给调用它的SQL语句。而OUT/INOUT型参数只会起到函数内部变量的作用,不会返回给调用它的SQL语句。
当被调用函数作为range table entry出现时,该函数返回值的类型可以是:标量类型、行类型(row type)、记录类型(record type)以及集合类型(set type)。需要注意的是,对于返回记录类型函数的调用,需要指明记录的内部结构。
示例:作为查询来源表¶
-- 清理环境
DROP FUNCTION func(INT);
create or replace function func(i int)
return SETOF record as
TYPE R1 IS RECORD
(
a int,
b int,
c int
);
TYPE R2 IS RECORD(
a int,
b numeric,
c text
);
retval1 R1;
retval2 R2;
BEGIN
IF i > 10 THEN
SELECT 5, 10, 15 INTO retval1;
RETURN NEXT retval1;
RETURN NEXT retval1;
ELSE
SELECT 50, 5::numeric, 'xxx'::text INTO retval2;
RETURN NEXT retval2;
RETURN NEXT retval2;
END IF;
RETURN;
END;
/
select * from func(1500) AS (a int, b int, c int);
A(int) |B(int) |C(int) |
------------------------------------
5 |10 |15 |
------------------------------------
5 |10 |15 |
总数目:2
--删除过程
DROP FUNCTION func(INT);