Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: query has no destination for result data

Tags:

sql

postgresql

i have created a function in PostgreSQL to insert to the following

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

and created Function is

  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 a execute the above function

select funcInsert(666,13,'2014-06-06'

  • getting this error

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

like image 892
Vivek S. Avatar asked Jun 06 '14 11:06

Vivek S.


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 as 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;
like image 79
Clodoaldo Neto Avatar answered Sep 27 '22 20:09

Clodoaldo Neto


This code is working:

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

And because it is same code as your, I expect, so you use some very old Postgres. I remember similar bug in pg, but it is more than five years fixed.

like image 26
Pavel Stehule Avatar answered Sep 27 '22 19:09

Pavel Stehule