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 に答える