POSTGRESQL-查询没有结果数据的目的地

POSTGRESQL- Query has no destination for result data

我是 postgres 和编程的新手,我已经为此搜索了解决方案,但我不太明白。我正在尝试制作一个函数,每当我致电该国家/地区时,该功能都会 return 有关该特定国家/地区所有客户的信息。这是弹出的错误。我真的很抱歉问这个问题,但我从昨天就被困在这里了。

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 country(text) line 5 at SQL statement

函数如下:

create or replace function country(text) returns text as $$
begin


  select customer_id, customer.first_name, customer.last_name 

  from customer 

  inner join address on customer.address_id = address.address_id 
  inner join city on address.city_id = city.city_id 
  inner join country on city.country_id = country.country_id 

  where country = '';
  end;
  $$

  language plpgsql;

请使用下面的方法获取给定函数的结果..

create or replace function country(in_parameter text,out out_res refcursor) as $$
begin
open out_res for
select customer_id, customer.first_name, customer.last_name 

from customer 

inner join address on customer.address_id = address.address_id 
inner join city on address.city_id = city.city_id 
inner join country on city.country_id = country.country_id 

where country = '';
end;
$$

language plpgsql;

这对于 SQL 来说是不正常的。通常,这将是 VIEW.

CREATE VIEW myView AS
    SELECT customer_id, customer.first_name, customer.last_name 
    FROM customer 
    INNER JOIN address USING (address_id)
    INNER JOIN city USING (city_id)
    INNER JOIN country USING (country_id);

那你就做

 SELECT * FROM myView WHERE country = ?

综上所述,如果您坚持将其设为函数,而您不应该将其设为 LANAGUAGE SQL 而不是 LANGUAGE plppsql

如果您在 PL/pgSQL 函数中执行 select 语句,那么您应该将查询结果放在某个变量(= 目标)中。然后你在函数中使用变量。您还应该有一个 RETURN 语句。

create or replace function country(text) returns text as $$
declare                   -- declare some variables
  id integer;
  fname text;
  lname text;
begin
  select customer_id, customer.first_name, customer.last_name 
    into id, fname, lname -- store query results in variables
  from customer 
  inner join address on customer.address_id = address.address_id 
  inner join city on address.city_id = city.city_id 
  inner join country on city.country_id = country.country_id 
  where country = ;     -- don't quote parameter references

  -- do something with the variables and return a value from the function
  return format('%s: %s %s', id, upper(lname), fname);
end;
$$ language plpgsql;

请注意,以上仅在查询 return 是单行时有效。如果查询return多行你可以在use a loop函数中。更简单的是,您可以像这样 return 查询结果:

create or replace function country(text)
returns table (id integer, first_name varchar, last_name varchar) as $$
begin
  return query
    select customer_id, customer.first_name, customer.last_name 
    from customer 
    inner join address on customer.address_id = address.address_id 
    inner join city on address.city_id = city.city_id 
    inner join country on city.country_id = country.country_id 
    where country = ;
end;
$$ language plpgsql;

但是就像 所说的那样,除非您需要 PL/pgSQL 函数在 return 之前修改数据,否则最好使用简单的视图。

我的同事在 select 查询之前使用 OPEN MYCURS 并且 RETURN MYCURS 在 select 查询之后。

在某些情况下,我们可能希望从一个函数中调用另一个 psql 函数。如果我们只是想调用该函数而不将 return 值分配给任何东西,那么在 main 函数中使用 select inner_function_call(); 将抛出此错误。正如提示所暗示的那样,请改用 perform inner_function_call()

CREATE OR REPLACE FUNCTIOn func_process_client_contact(p_client_version_id bigint, p_publish_id bigint)
    RETURNS TABLE(status_message character varying)
    LANGUAGE plpgsql
AS $function$
declare 
    p_rowcount int;
    BEGIN
        -- validating input parameters : start
        IF ( p_client_version_id IS NULL OR p_publish_id IS NULL )
        THEN 
            RETURN;
        END IF;
        -- validating input parameters : end 

       WITH cte_target AS
       (
            SELECT  
            g.name AS clientname,
            gcr.group_id AS clientid,
            cr.id AS clientrelationid,
            crc.id AS clientrelationcontactid,
            crd.id AS clientrelationdesignation_id
            FROM GROUPS g
            JOIN group_client_relation gcr ON gcr.group_id =g.id
            JOIN client_relation cr ON cr.id = gcr.client_relation_id 
            JOIN client_relation_contact crc ON crc.client_relation_id =cr.id 
            JOIN client_relation_designation crd ON cr.client_relation_designation_id =crd.id
        )
        SELECT * FROM cte_target ct WHERE ct.clientname='ABC';
        GET DIAGNOSTICS p_rowcount = ROW_COUNT;
        RAISE NOTICE 'returned % rows', p_rowcount;
        IF ( p_rowcount=0 )
        THEN 
           RETURN query SELECT 'hello' AS status_message;
        ELSE
            RETURN query SELECT 'Success' AS status_message;
        END IF; 
    END
$function$