PostgreSQL 函数 return 类型记录显示错误
PostgreSQL function return type record showing error
我已成功创建以下 table 和 function.function 但是当我调用
功能,它显示错误。
create table emp_details(empno int,ename varchar(20),sal numeric(7,2))
insert into emp_details values(101,'John',4500)
insert into emp_details values(101,'david',5000)
CREATE OR REPLACE FUNCTION test_select(IN eno integer)
RETURNS SETOF RECORD AS
$body$
BEGIN
RETURN QUERY
SELECT ename,sal FROM EMP_DETAILS WHERE EMPNO=eno;
END;
$body$
LANGUAGE plpgsql
调用 function.it 时创建的函数 successfully.But 正在显示 error.how 我们可以在记录类型中实现吗?
select test_select(101)
改用returns table
,这样您在使用该函数时就不需要指定列名。而且你不需要 PL/pgSQL 为此,如果你只想包装查询,language sql
函数会更有效率。
CREATE OR REPLACE FUNCTION test_select(IN eno integer)
RETURNS TABLE (ename text, sal numeric)
AS
$body$
SELECT ename,sal
FROM emp_details
WHERE EMPNO=eno;
END;
$body$
LANGUAGE sql;
设置返回函数(无论定义为returns setof
还是带returns table
)需要在FROM子句中使用,所以需要这样使用:
select *
from test_select(101);
我已成功创建以下 table 和 function.function 但是当我调用 功能,它显示错误。
create table emp_details(empno int,ename varchar(20),sal numeric(7,2))
insert into emp_details values(101,'John',4500)
insert into emp_details values(101,'david',5000)
CREATE OR REPLACE FUNCTION test_select(IN eno integer)
RETURNS SETOF RECORD AS
$body$
BEGIN
RETURN QUERY
SELECT ename,sal FROM EMP_DETAILS WHERE EMPNO=eno;
END;
$body$
LANGUAGE plpgsql
调用 function.it 时创建的函数 successfully.But 正在显示 error.how 我们可以在记录类型中实现吗?
select test_select(101)
改用returns table
,这样您在使用该函数时就不需要指定列名。而且你不需要 PL/pgSQL 为此,如果你只想包装查询,language sql
函数会更有效率。
CREATE OR REPLACE FUNCTION test_select(IN eno integer)
RETURNS TABLE (ename text, sal numeric)
AS
$body$
SELECT ename,sal
FROM emp_details
WHERE EMPNO=eno;
END;
$body$
LANGUAGE sql;
设置返回函数(无论定义为returns setof
还是带returns table
)需要在FROM子句中使用,所以需要这样使用:
select *
from test_select(101);