ERROR: request has no destination for result data

I created a function in PostgreSQLto insert the following

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

and created 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;
  • when performing the above function

select funcInsert (666.13, '2014-06-06'

  • getting this error

ERROR: query has no destination for CONTEXT result data: PL / pgSQL procgtab83 function (integer, integer, date) line 3 in SQL statement

+4
source share
2 answers
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;

It can be much simpler than plain 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;
+5
source

This code works:

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 line)

, , , Postgres. pg, ​​ .

+4

Source: https://habr.com/ru/post/1543553/


All Articles