Vivek S.
Vivek S.

Reputation: 21895

ERROR: query has no destination for result data

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;

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

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

Upvotes: 5

Views: 4421

Answers (2)

Pavel Stehule
Pavel Stehule

Reputation: 45770

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.

Upvotes: 4

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125214

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;

Upvotes: 6

Related Questions