首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >POSTGRESQL-查询没有结果数据的目的地。

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

Stack Overflow用户
提问于 2016-12-22 01:04:54
回答 6查看 45.8K关注 0票数 7

我对postgres和编程都很陌生,我已经为这个问题寻找了解决方案,但我无法完全理解。我试图建立一个函数,每当我打电话给这个国家时,它将返回关于那个特定国家的所有客户的信息。这就是弹出的错误。我很抱歉问了这个问题,但从昨天起我就一直困在这里。

错误:查询没有结果数据的目标 提示:如果要放弃选择的结果,请使用“执行”。 上下文: PL/pgSQL函数国家(文本) SQL语句第5行

以下是功能:

代码语言:javascript
运行
复制
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 = '$1';
  end;
  $$

  language plpgsql;
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-12-22 07:35:42

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

代码语言:javascript
运行
复制
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 = $1;     -- 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;

请注意,只有当查询返回单个行时,上面的操作才有效。如果查询返回多行,则可以在函数中使用循环。更简单的是,您可以返回查询的结果,如下所示:

代码语言:javascript
运行
复制
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 = $1;
end;
$$ language plpgsql;

但是就像埃文·卡罗尔说的,除非您需要一个PL/pgSQL函数来在返回数据之前修改数据,否则最好使用一个简单的视图。

票数 16
EN

Stack Overflow用户

发布于 2021-11-22 13:26:08

代码语言:javascript
运行
复制
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$
票数 1
EN

Stack Overflow用户

发布于 2016-12-22 04:48:17

请用下面的方法得到给定函数的结果..

代码语言:javascript
运行
复制
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 = '$1';
end;
$$

language plpgsql;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41274337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档