1
CREATE OR REPLACE FUNCTION master.test2(tehsil text, district text, state text, flag text)
  RETURNS setof master.population AS
$BODY$
BEGIN


  IF flag='A' THEN
    select Count(*) from master.population where income in('1','2')and statecode=state;
  ELSIF flag='B' THEN
 select Count(*) from master.population where income in('1','2') and statecode=state and districtcode=district ;
   ELSIF flag='C' THEN
  select Count(*) from master.population where income in('1','2') and statecode=state and districtcode=district and tehsilcode=tehsil;


ELSE
     select Count(*) from master.population where income in('1','2'); 
END IF;

END;
$BODY$
  LANGUAGE plpgsql

Throwing an error:

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 master.test2(text,text,text,text) line 6 at SQL statement
2
  • You should also add what you have tried to fix your problem. Commented May 22, 2013 at 9:01
  • What's unclear about the error message? The result of the select count(*) needs to be stored in a variable. If you want to return a result, you need to use return query. Please check the manual, it has several examples for this. Commented May 22, 2013 at 9:23

2 Answers 2

5

plpgSQL function can't just run a query; you have to put the results somewhere.
See the documentation for details.

It appears that you want to return the count(*) from the function so your return type should look something like

CREATE OR REPLACE FUNCTION master.test2(tehsil text, district text, state text, flag text)    RETURNS INT AS
$BODY$
  DECLARE 
    CNT INT;
  BEGIN

  IF flag='A' THEN
    select Count(*) from master.population where income in('1','2')and statecode=state INTO CNT;
  ELSIF flag='B' THEN
 select Count(*) from master.population where income in('1','2') and statecode=state and districtcode=district INTO CNT ;
   ELSIF flag='C' THEN
  select Count(*) from master.population where income in('1','2') and statecode=state and districtcode=district and tehsilcode=tehsil INTO CNT;

ELSE
     select Count(*) from master.population where income in('1','2') INTO CNT; 
END IF;
RETURN CNT;

END;
$BODY$
LANGUAGE plpgsql;
Sign up to request clarification or add additional context in comments.

3 Comments

Postgres functions can return a result set, they just need to use return query. But in this case this is not needed as only a single value should be returned.
so if I want to call this function, it should be like this??? select * from master.test2('0', '0', '04', 'C') but when I am calling it like this it's giving a error 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 secc_master.test3(text,text,text,text) line 17 at SQL statement ********** Error ********** ERROR: query has no destination for result data SQL state: 42601
@Muhammad Usama: usage of into in the last for assignment query result is wonderful for me. actually I was searching to save auto generated id into variable and then return it.But always error when calling function. but by following way(using Into in last) did work for me. 'Insert into usertbl(id, username) values (default, user_name) returning id into lastid;' lastid was declared as bigint. Thanks and +1 for this.
0

Use:

RETURN QUERY select Count(*) ...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.