From: jimmy.olsen <jimmy.olsen@ig.com.br>
To: pgsql-sql@postgresql.org
Subject: Re: getting back autonumber just inserted
Date: Sat, 9 Jul 2005 09:59:58 -0300
Message-ID: <007901c58486$5a973430$230a0a0a@ligaone.com.br> (raw)
References: <42CD6B58.6060508@techevolution.be>
<20050707180329.GA11332@alvh.no-ip.org>
I don't know how to create a Global Variable in Postgres, but the idea is
very simple:
1. Encapsulate the NextVal function in MyNextVal
2. Set to Global variable with NextVal of the desired sequence
3. Inspect to value of the global variable (exactally as it's done in MsSQL)
When Creating a Table:
CREATE TABLE tabTest(
codTest Int Not Null PRIMARY KEY DEFAULT(NextVal("seq_test"),
... # Field List
)
Try this:
CREATE TABLE tabTest(
codTest Int Not Null PRIMARY KEY DEFAULT(MyNextVal("seq_test"),
... # Field List
)
this is not the correct sintax, just an algoritm
CREATE FUNCTION MyNextVal(varchar) RETURNS INT AS
$$ SELECT GlobalVar = NextVal($1)
RETURN GlobalVar
$$
Alessandro - Liga One Sistemas
alessandro@ligaone.com.br
----- Original Message -----
From: "Alvaro Herrera" <alvherre@alvh.no-ip.org>
To: "mail TechEvolution" <mail@techevolution.be>
Cc: <pgsql-sql@postgresql.org>
Sent: Thursday, July 07, 2005 3:03 PM
Subject: Re: [SQL] getting back autonumber just inserted
> On Thu, Jul 07, 2005 at 07:50:16PM +0200, mail TechEvolution wrote:
> > hello
> >
> > i ame a newbie to PostGreSQL, I ame using PostGreSQL 8.0
> > (windowsinstaller) on a xp prof platform
> >
> > i would like to get back the autonumber from the last record inserted,
> > for other SQL db (m$ sql db ...) i could use:
> > SELECT @@ IDENTITY
> >
> > can someone help me by informing me what the SQL syntax is to be used
> > with PostGreSQL db and get the same result, the last autonumber
inserted?
>
> You use the currval() function, using the name of the involved sequence
> as parameter. There is a pg_get_serial_sequence() function, to which
> you give the table name and column name, and it will give you the
> sequence name.
>
> --
> Alvaro Herrera (<alvherre[a]alvh.no-ip.org>)
> Oh, oh, las chicas galacianas, lo harán por las perlas,
> ¡Y las de Arrakis por el agua! Pero si buscas damas
> Que se consuman como llamas, ¡Prueba una hija de Caladan! (Gurney Halleck)
>
> ---------------------------(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
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jimmy.olsen@ig.com.br
Subject: Re: getting back autonumber just inserted
In-Reply-To: <007901c58486$5a973430$230a0a0a@ligaone.com.br>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox