查询没有结果数据错误的目的地

时间:2018-01-11 12:09:14

标签: postgresql plpgsql

我在`

中有以下功能
CREATE OR REPLACE FUNCTION public.get_avalable_providers(
  start_day_id integer,
  end_day_id integer,
  number_of_days integer,
  requested integer)
    RETURNS SETOF provider AS
$BODY$declare
required integer;
available_product integer;
p provider;
p_id integer;
noa integer;
begin
   FOR p IN SELECT * FROM provider
   loop     
      FOR p_id, noa IN SELECT id, number_of_availables FROM product
         WHERE provider_id = p.id
   LOOP
    required = requested/noa;
                      select available_product = 
                   public.get_available_products_biggerthan(
        start_day_id, end_day_id, number_of_days, required, p_id);
     if available_product = number_of_days then
          return next p;
            exit;         
         end if;
      END LOOP;  
  end loop;   

  return;
  end;         
  $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
 ROWS 1000;
 ALTER FUNCTION public.get_avalable_providers(integer, integer, integer, 
      integer, integer)
  OWNER TO postgres;

上述函数应该返回一些在一天内具有足够数量的请求product的提供程序 它正在利用另一个函数get_available_products_biggerthan,我收到以下错误:

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 
get_avalable_products(integer,integer,integer,integer,integer) line 15 at 
SQL statement

问题 我哪里弄错了?

1 个答案:

答案 0 :(得分:2)

https://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW

  

SQL命令产生单行的结果(可能是   多列)可以分配给记录变量,行类型   变量或标量变量列表。这是通过写作来完成的   基本SQL命令并添加INTO子句

尝试下面(我只修复了明显的语法):

CREATE OR REPLACE FUNCTION public.get_avalable_providers(
  start_day_id integer,
  end_day_id integer,
  number_of_days integer,
  requested integer)
    RETURNS SETOF provider AS
$BODY$declare
required integer;
available_product integer;
p provider;
p_id integer;
noa integer;
begin
   FOR p IN (SELECT * FROM provider)
   loop     
      FOR p_id, noa IN (SELECT id, number_of_availables FROM product
         WHERE provider_id = p.id)
   LOOP
    required = requested/noa;
    select public.get_available_products_biggerthan(
        start_day_id, end_day_id, number_of_days, required, p_id) INTO available_product ;
     if available_product = number_of_days then
          return next p;
            exit;         
         end if;
      END LOOP;  
  end loop;   

  return;
  end;         
  $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
 ROWS 1000;