Re: GET LAST ID INSERT

From: "Marek Lewczuk" <newsy(at)lewczuk(dot)com>
To: "'Maurizio Faini'" <maufaini(at)tiscali(dot)it>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: GET LAST ID INSERT
Date: 2003-09-24 08:04:49
Message-ID: 006f01c38272$86c843e0$4701a8c0@marek
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)postgresql(dot)org
> [mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of
> Maurizio Faini
> Sent: Tuesday, September 23, 2003 9:25 AM
> To: pgsql-general(at)postgresql(dot)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
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ian Harding 2003-09-24 08:31:20 Re: pltcl problem
Previous Message Shridhar Daithankar 2003-09-24 06:55:11 OO and RDBMS