Re: SQL stored function inserting and returning data in a row. - Mailing list pgsql-sql

From Gerardo Herzig
Subject Re: SQL stored function inserting and returning data in a row.
Date
Msg-id 47874CEB.3010803@fmed.uba.ar
Whole thread Raw
In response to SQL stored function inserting and returning data in a row.  ("Daniel Caune" <daniel.caune@ubisoft.com>)
Responses Re: SQL stored function inserting and returning data in a row.  ("Daniel Caune" <daniel.caune@ubisoft.com>)
List pgsql-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;

?


pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: trigger for TRUNCATE?
Next
From: "Marcin Stępnicki"
Date:
Subject: Re: SQL stored function inserting and returning data in a row.