0

Following is a Function created in PostgreSQL 9.2 ,

CREATE OR REPLACE FUNCTION getRows()
RETURNS TABLE (
        "Name" character varying
       ,"Address" text
       ,"Phone" text
       ,"Email" character varying
       ,"Tin" character varying
       ,"DL.No1, DL.No2" text
       ,"Area" character varying
) AS
$func$
BEGIN
       select  acname as "Name", coalesce(nullif(concat_ws(', ', nullif(add1, ''),  
       nullif(add2, '')), ''), 'NIL') "Address", coalesce(nullif(concat_ws(', ', 
       nullif(phoneoff, ''), nullif(mobile, '')), ''), 'NIL') "Phone",case when email 
       <>'' then email else 'NIL' end as "Email",case when email <>'' then tin else 
       'NIL' end as "Tin",case when dlno1 ||', '||dlno2=', ' then 'NIL' else dlno1 ||',  
       '||dlno2 end as "DL.No1, DL.No2",areaname as "Area" from gtab12   left  join 
      gtab47 using(areaid) where acgrcode  = '204'  order by areaname,"Name";
END
$func$  LANGUAGE plpgsql;


When select getRows() an error is occurring

     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 getRows() line 4 at SQL statement 

given below is actually what the select query inside the function getRow returns. enter image description here

2 Answers 2

2

You cannot use a SELECT statement in a plpgsql function without the INTO clause (you must use PERFORM instead).

You also forget to RETURN with something.

But, if your function is that simple, you could use a plain sql function (or even a view) instead.

Sign up to request clarification or add additional context in comments.

Comments

1

To return a set of something, you need to use RETURN NEXT (to return a row at a time) or RETURN QUERY (to return a number of rows resulting from a query).

See the manual section relating to returning values.

Your example function would need to have:

RETURN QUERY SELECT....;
RETURN;

Note the need for the second RETURN - this is needed because there may be multiple RETURN QUERY statements before the function exits.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.