Re: GET LAST ID INSERT

Поиск
Список
Период
Сортировка
От Marek Lewczuk
Тема Re: GET LAST ID INSERT
Дата
Msg-id 006f01c38272$86c843e0$4701a8c0@marek
обсуждение исходный текст
Ответ на GET LAST ID INSERT  (maufaini@tiscali.it (Maurizio Faini))
Ответы Re: GET LAST ID INSERT  (Andrew Sullivan <andrew@libertyrms.info>)
Список pgsql-general
I'm using PHP in my App, and PHP have last_oid function - using this
function I've wrote function in plpgsql to get last inserted id:

CREATE FUNCTION "public"."last_id" (bigint, varchar, varchar) RETURNS
text AS'
DECLARE
  var_result RECORD;
  var_query  VARCHAR;
  var_id     TEXT;
BEGIN
  var_query := ''SELECT '' || $3 ||'' AS __id FROM '' || $2 || '' WHERE
oid = '' || $1 || '';'';
  IF $1 > 0 THEN
     FOR var_result IN EXECUTE var_query LOOP
       var_id := var_result.__id;
     END LOOP;
     IF var_id > 0 THEN
       RETURN var_id;
     ELSE
       RETURN NULL;
     END IF;
  END IF;
END;
'LANGUAGE 'plpgsql' VOLATILE RETURNS NULL ON NULL INPUT SECURITY
INVOKER;

Where:
Var $1 is "oid number"
Var $2 is "table_name"
Var $3 is "id_field_name"

Of course we can change this function and get table name automaticaly...
But in my case there is was need for this... But you can try.

Best wishes,
ML


> -----Original Message-----
> From: pgsql-general-owner@postgresql.org
> [mailto:pgsql-general-owner@postgresql.org] On Behalf Of
> Maurizio Faini
> Sent: Tuesday, September 23, 2003 9:25 AM
> To: pgsql-general@postgresql.org
> Subject: [GENERAL] GET LAST ID INSERT
>
>
> I have a little problem.
>
> there is a way to get last id inserted into db or i have to
> make a new query?
>
> I explain better my question:
>
> in vbscript using sqlserver2000 i can use this code:
>
> varBookmark=rs.Bookmark
> rs.Update
> rs.Bookmark=varBookmark
>
> in this way in I the new id just inserted!!!
>
> how can i do this with postgres?
> there is a way?
>
> thanks
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>




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

Предыдущее
От: Shridhar Daithankar
Дата:
Сообщение: OO and RDBMS
Следующее
От: "Arjen van der Meijden"
Дата:
Сообщение: Re: [ADMIN] Fatal error: Allowed memory size of 8388608