Skip site navigation (1) Skip section navigation (2)

Re: SQL stored function inserting and returning data in a row.

From: Gerardo Herzig <gherzig(at)fmed(dot)uba(dot)ar>
To: Daniel Caune <daniel(dot)caune(at)ubisoft(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL stored function inserting and returning data in a row.
Date: 2008-01-11 11:03:07
Message-ID: 47874CEB.3010803@fmed.uba.ar (view raw or flat)
Thread:
Lists: pgsql-adminpgsql-sql
Daniel Caune wrote:

>Hi,
>
>Is there any way to define a SQL stored function that inserts a row in a
>table and returns the serial generated?
>
>CREATE TABLE matchmaking_session
>(
>  session_id bigint NOT NULL DEFAULT
>nextval('seq_matchmaking_session_id'),
>  ...
>);
>
>CREATE FUNCTION create_matchmaking_sesssion(...)
>  RETURNS bigint
>AS $$
>  INSERT INTO matchmaking_session(...)
>    VALUES (...)
>    RETURNING session_id;
>$$ LANGUAGE SQL;
> 
>2008-01-10 22:08:48 EST ERROR:  return type mismatch in function
>declared to return bigint
>2008-01-10 22:08:48 EST DETAIL:  Function's final statement must be a
>SELECT.
>2008-01-10 22:08:48 EST CONTEXT:  SQL function
>"create_matchmaking_sesssion"
>
>  
>
What about
$$
INSERT INTO .... ;
select currval('seq_matchmaking_session_id');
$$ language sql;

?

In response to

Responses

pgsql-admin by date

Next:From: Hocine AbirDate: 2008-01-11 11:06:45
Subject: Re: [SQL] SQL stored function inserting and returning data in arow.
Previous:From: Pedro Briones GarcíaDate: 2008-01-11 10:22:02
Subject: PostgreSQL on 64-bit Windows

pgsql-sql by date

Next:From: Hocine AbirDate: 2008-01-11 11:06:45
Subject: Re: [SQL] SQL stored function inserting and returning data in arow.
Previous:From: Richard HuxtonDate: 2008-01-11 08:24:35
Subject: Re: trigger for TRUNCATE?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group