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

From Marc Mamin
Subject Re: SQL stored function inserting and returning data in a row.
Date
Msg-id CA896D7906BF224F8A6D74A1B7E54AB301750D2D@JENMAIL01.ad.intershop.net
Whole thread Raw
In response to Re: SQL stored function inserting and returning data in a row.  ("Daniel Caune" <daniel.caune@ubisoft.com>)
List pgsql-sql
> What about
> $$
> INSERT INTO .... ;
> select currval('seq_matchmaking_session_id');
> $$ language sql;
>
> ?

Hello,

I'm not sure that this would return the correct id in case of concurrent
calls to your function.
I'm using following kind of function to manage reference tables:

HTH,

Marc Mamin


CREATE TABLE xxx
( id serial NOT NULL, mycolumn character varying, CONSTRAINT xxx_pk PRIMARY KEY (id) , CONSTRAINT xxx_uk UNIQUE
(mycolumn)
)



CREATE OR REPLACE FUNCTION get_or_insert_id_xxx( input_value varchar)
RETURNS INT AS $$

DECLARE id_value int;

BEGIN select into id_value id from xxx where mycolumn =  input_value; IF FOUND THEN   return id_value; ELSE   insert
intoxxx ( mycolumn ) values (  input_value );   return id from xxx where mycolumn =  input_value; END IF; 

EXCEPTION WHEN unique_violation THEN   return id from xxx where mycolumn =  input_value;

END;
$$ LANGUAGE plpgsql;


pgsql-sql by date:

Previous
From: "Marc Mamin"
Date:
Subject: Re: SQL question: Highest column value of unique column pairs
Next
From: "Peter Childs"
Date:
Subject: Re: trigger for TRUNCATE?