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
答案 0 :(得分:5)
plpgSQL函数不能只运行查询;你必须把结果放在某个地方 See the documentation了解详情。
您希望从函数返回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;
答案 1 :(得分:0)
使用:
RETURN QUERY select Count(*) ...