Re: how to get the primary key of a freshly inserted row

Поиск
Список
Период
Сортировка
От Darren Ferguson
Тема Re: how to get the primary key of a freshly inserted row
Дата
Msg-id Pine.LNX.4.44.0208052043580.17335-100000@thread.crystalballinc.com
обсуждение исходный текст
Ответ на Re: how to get the primary key of a freshly inserted row in a stored procedure  (Lee Harr <missive@frontiernet.net>)
Список pgsql-general
The way you are doing it will work fine

The other way is your could return CURRVAL('id_seq');

HTH

On Tue, 6 Aug 2002, Lee Harr wrote:

> > I'm writing a PL/pgSQL function that will insert a row and return its
> > id. Right now I just do a select after the insert to get the id of the
> > new row (see example code below). But I'm guessing that there's a
> > better way. Any recommendations?
>
> It would help to see your table definitions, but I am thinking
> something like this might work... (this assumes that id uses
> a sequence for its values, like a SERIAL type.)
>
> > CREATE FUNCTION foo(VARCHAR, VARCHAR)
> > RETURNS INTEGER
> > AS '
> >    DECLARE
> >      p1 ALIAS FOR $1;
> >      p2 ALIAS FOR $2;
> >      v_id INTEGER;
> >    BEGIN
>        select nextval(''id_seq'') into v_id;
> >      INSERT INTO foo (id, a, b) VALUES (v_id, p1, p2);
> >    RETURN v_id;
> >    END;
> > '
> > LANGUAGE 'plpgsql';
> >
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>

--
Darren Ferguson


В списке pgsql-general по дате отправления:

Предыдущее
От: Lee Harr
Дата:
Сообщение: Re: how to get the primary key of a freshly inserted row in a stored procedure
Следующее
От: Sean Chittenden
Дата:
Сообщение: Re: O'Reilly Open Source Convention Report