Question

I have one function that returns all employee IDs Function definition is like this:

CREATE OR REPLACE FUNCTION tmp()
  RETURNS setof record AS
$func$
begin
    select emp_id from employee_master;
end;
$func$
  LANGUAGE plpgsql;

But when i call this function using

select * from tmp() as abc(emp_id text);

It gives error like

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "tmp" line 3 at SQL statement

Please give solution :)

Was it helpful?

Solution

If you want to return a rowset from a PL/PgSQL function you must use RETURN - in this case, probably RETURN QUERY:

RETURN QUERY SELECT emp_id FROM employee_master;

I don't see the point of having this in a PL/PgSQL function at all, though.

OTHER TIPS

Make the function a plain SQL one as in:

...
LANGUAGE SQL;

It is much more practical to declare the actual type of the column instead of the unwieldy record type. Assuming emp_id to be integer, a simple SQL function could look like this:

CREATE OR REPLACE FUNCTION tmp()
  RETURNS SETOF integer AS
$func$
   SELECT emp_id FROM employee_master
$func$  LANGUAGE sql;

However, the error message in your comment does not match the given question. Depending on your actual requirements, you would adjust the RETURN type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top