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

抛出错误:

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
4

2 回答 2

5

plpgSQL 函数不能只运行查询;你必须把结果放在某个地方。
有关详细信息,请参阅文档

看来您想count(*)从函数返回,所以您的返回类型应该类似于

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;
于 2013-05-22T09:20:22.493 回答
0

利用:

RETURN QUERY select Count(*) ...
于 2014-06-07T13:43:26.957 回答