【问题标题】:ERROR: query has no destination for result data错误:查询没有结果数据的目的地
【发布时间】:2014-07-27 16:14:57
【问题描述】:

我在PostgreSQL 中创建了一个函数来插入到下面

  CREATE TABLE gtab83
(
  orderid integer NOT NULL DEFAULT nextval('seq_gtab83_id'::regclass),
  acid integer,
  slno integer,
  orderdte date
)

并创建Function

  CREATE OR REPLACE FUNCTION funcInsert(iacid int,islno int,idate date) RETURNS int AS

$$

declare id_val int;

BEGIN

    INSERT INTO GTAB83 (acid,slno,orderdte) VALUES (iacid,islno,idate) RETURNING orderid 
into id_val;

return id_val;

END;

$$

  LANGUAGE plpgsql;
  • 执行上述函数时

选择 funcInsert(666,13,'2014-06-06'

  • 收到此错误

错误:查询没有结果数据的目的地 CONTEXT: PL/pgSQL 函数 procgtab83(integer,integer,date) 第 3 行的 SQL 语句

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:
    create or replace function funcinsert(iacid int, islno int, idate date)
    returns int as $$
    
    declare id_val int;
    
    begin
        with i as (
            insert into gtab83 (acid,slno,orderdte)
            values (iacid,islno,idate)
            returning orderid
        )
        select orderid into id_val
        from i;
        return id_val;
    end;
    $$ language plpgsql;
    

    它可以像普通的sql一样简单

    create or replace function funcinsert(iacid int, islno int, idate date)
    returns int as $$
    
        insert into gtab83 (acid,slno,orderdte)
        values (iacid,islno,idate)
        returning orderid
        ;
    $$ language sql;
    

    【讨论】:

      【解决方案2】:

      此代码正在运行:

      postgres=# create table xx(a int);
      CREATE TABLE
      
      postgres=# create or replace function bu(int) returns int as 
                   $$declare x int; 
                   begin 
                     insert into xx values($1) returning a into x; 
                     return x; 
                   end $$ language plpgsql;
      CREATE FUNCTION
      
      postgres=# select bu(10);
       bu 
      ────
       10
      

      (1 行)

      因为它与你的代码相同,我希望你使用一些非常旧的 Postgres。我记得 pg 里有类似的 bug,但它已经修复了五年多。

      【讨论】:

        猜你喜欢
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多