【问题标题】:No data output from stored procedure (Postgresql)存储过程没有数据输出(Postgresql)
【发布时间】:2020-10-15 07:31:46
【问题描述】:

我有一个用于选择语句的基本存储过程。 select 语句本身可以工作并向我显示数据输出,但是当我尝试从存储过程调用它时,它显示“CALL Query 在 107 毫秒内成功返回”,但没有数据输出。我的存储过程中是否缺少某些内容? (我还将 AWS 上的数据库与基本层连接起来,不确定这是否会有所不同。除 select 外,其他所有 CRUD 操作都有效。)

CREATE PROCEDURE 
    experiment1()
LANGUAGE SQL
AS $$
    SELECT * FROM assignment    
$$

【问题讨论】:

    标签: sql postgresql select stored-procedures sql-function


    【解决方案1】:

    Postgres 过程不返回任何内容。您可以改用函数,使用 return query 语法。这需要枚举查询返回的列。假设您的表有两列,idval,那就是:

    create function experiment() 
    returns table (id int, val text)
    as $$
    begin
        return query select * from assignment;
    end;
    $$ language plpgsql;
    

    然后你可以像这样调用这个返回集合的函数:

    select * from experiment();
    

    Demo on DB Fiddle

    create table assignment (id int, val text);
    insert into assignment values(1, 'foo'), (2, 'bar');
    -- 2 rows affected
    
    create function experiment() 
    returns table (id int, val text)
    as $$
    begin
        return query select * from assignment;
    end;
    $$ language plpgsql;
    
    select * from experiment();
    
    编号 |值 -: | :-- 1 |富 2 |酒吧

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 2013-12-01
      相关资源
      最近更新 更多