为什么我在执行 Postgres 函数时出现此错误

Why iam getting this error while executing Postgres function

CREATE OR REPLACE FUNCTION public.flowrate7(
    )
    RETURNS TABLE(oms_id integer, flowrate numeric, chakno character varying) 
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$

    declare temp_omsId integer;
    declare temp_flowrate numeric(20,3);
    declare temp_chakno varchar(100);

begin

DROP TABLE IF EXISTS tbl_oms;
DROP TABLE IF EXISTS tbl_calFlow;

        CREATE temporary TABLE tbl_oms(omsid__ integer) ON COMMIT DELETE ROWS;
        CREATE temporary TABLE tbl_calFlow(OmsId_ integer,FlowRate_ numeric(20,3),ChakNo_ varchar(100)) ON COMMIT DELETE ROWS;

            insert into tbl_oms (select OmsId from MstOms);

            while (select count(*) from tbl_oms) <> 0 LOOP

            select temp_omsId = omsid__ from tbl_oms LIMIT 1;


        temp_flowrate = (select (case when(InLetPressure > 0.5) then 1 else 0 end) from MstOms where OmsId = temp_omsId);
        temp_chakno = (select ChakNo  from MstOms where OmsId = temp_omsId);

                insert into tbl_calFlow values (temp_omsId,temp_flowrate,temp_chakno);

                delete from tbl_oms where omsid__ = temp_omsId; 

            END LOOP;

            return query (select OmsId_,FlowRate_,ChakNo_ from tbl_calFlow);
end;
$BODY$;

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 flowrate7() line 19 at SQL statement SQL state: 42601

您错误地将值检索到变量中。要将查询结果的值存储到一个(或两个)变量中,您需要使用 select .. into variable ...

CREATE OR REPLACE FUNCTION public.flowrate7()
    RETURNS TABLE(oms_id integer, flowrate numeric, chakno character varying) 
    LANGUAGE plpgsql 
AS $BODY$

declare 
  temp_omsId integer;
  temp_flowrate numeric(20,3);
  temp_chakno varchar(100);
begin

  DROP TABLE IF EXISTS tbl_oms;
  DROP TABLE IF EXISTS tbl_calFlow;

  CREATE temporary TABLE tbl_oms(omsid__ integer) ON COMMIT DELETE ROWS;
  CREATE temporary TABLE tbl_calFlow(OmsId_ integer,FlowRate_ numeric(20,3),ChakNo_ varchar(100)) ON COMMIT DELETE ROWS;

  insert into tbl_oms 
  select OmsId 
  from MstOms;

  while (select count(*) from tbl_oms) <> 0 LOOP

    select omsid__  
       into temp_omsId   --<< here
    from tbl_oms LIMIT 1;

    select case when inletpressure> 0.5 then 1 else 0 end, chakno 
      into temp_flowrate, temp_chakno --<< here
    from MstOms 
    where omsid = temp_omsId;

    insert into tbl_calFlow values (temp_omsId,temp_flowrate,temp_chakno);
    delete from tbl_oms where omsid__ = temp_omsId; 

  END LOOP;

  return query select omsid_, flowrate_, chakno_ 
               from tbl_calflow;
end;
$BODY$;

然而,该函数的处理不必要地复杂

  • if 首先将所有行 MstOms 复制到 tmp_MstOms
  • 从 tbl_oms
  • 中检索一行的 ID
  • 从 MstOms 中检索一行计算流量
  • 将那一行存储在临时文件中 table
  • 从(其他)临时文件中删除刚刚处理的行table
  • 计算 tbl_oms 中的行数,如果不为零,则移至 "next" 行

这是一种非常低效且复杂的计算简单值的方法,对于大 tables 无法很好地扩展。

在数据库中逐行处理 tables 是一种反模式开始(通过删除、插入和计算行来执行此操作会使速度更慢)。

这不是 SQL 中的处理方式。整个低效循环可以替换为单个查询,这也允许您将整个事情更改为 SQL 函数。

CREATE OR REPLACE FUNCTION public.flowrate7()
    RETURNS TABLE(oms_id integer, flowrate numeric, chakno character varying) 
    LANGUAGE sql
AS $BODY$
  select omsid, 
         case when inletpressure> 0.5 then 1 else 0 end as flowrate, 
         chakno 
  from mstoms;    
$BODY$;

实际上视图更适合这个。

create or replace view flowrate7
as
select omsid, 
       case when inletpressure> 0.5 then 1 else 0 end as flowrate, 
       chakno 
from mstoms;