I have the following table with two fields:
create table tbl_jtest
(
cola int,
colb varchar(10)
);
Inserting some records:
insert into tbl_jtest values(1,'a');
insert into tbl_jtest values(2,'b');
insert into tbl_jtest values(3,'c');
insert into tbl_jtest values(4,'d');
Function:
CREATE OR REPLACE FUNCTION ufn_jtest1(pcola int)
RETURNS json AS
$$
BEGIN
IF pcola = 1
THEN
RETURN QUERY SELECT to_json(a.cola) FROM tbl_jtest a;
ELSE
RETURN QUERY SELECT to_json(a.colb) FROM tbl_jtest a;
END IF;
END;
$$ LANGUAGE plpgsql;
Error details:
ERROR: cannot use RETURN QUERY in a non-SETOF function LINE 7: RETURN QUERY SELECT to_json(a.cola) FROM tbl_jtest a; ^
I have tried the followings:
Try 1:
PERFORM to_json(a.cola) FROM tbl_jtest a;
Try 2:
RETURN QUERY PERFORM to_json(a.cola) FROM tbl_jtest a;
RETURN (SELECT to_json(a.cola) FROM tbl_jtest a);
- what is you r goal anyway?.. – Vao Tsuntbl_jtest
table suggests that you indeed want aRETURNS SETOF json
function instead (like PostgreSQL suggests). If the table contains multiple rows, aRETURNS json
function will fail (unless you use aWHERE
and/or aLIMIT 1
clause). – pozscola,colb
in select statement. – MAK