Skip site navigation (1) Skip section navigation (2)

Re: nextval/dbi question

From: harrold(at)sage(dot)che(dot)pitt(dot)edu
To:
Cc: Pgsql Novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: nextval/dbi question
Date: 2001-08-05 22:47:22
Message-ID: Pine.LNX.4.21.0108051836560.10373-100000@sage.che.pitt.edu (view raw or flat)
Thread:
Lists: pgsql-novice
Sometime in August Horst Herb assaulted keyboard and produced...

|On Monday 06 August 2001 06:55, you wrote:
|
|> this is what i'm doing:
|> # i get the new value like this:
|> select nextval('testtable_the_key_seq');
|
|you are not assigning it to any variable?
|
|> #and then i preforme the insert like this:
|> insert into testtable (an_id, timestamp) values (2, 2);
|
|I assume that an_id is not your primary key?
|
|> say the last value of  the sequence is 'n-1' so the select nextval
|> statement above will return 'n' but when i preform the insert the value of
|> testtable.the_key is actually 'n+1'. should i wrap the insert up into a
|> transaction? any help would be most appreciated.
|>
|> i dont know if this is relevent but in perl i'm preparing the statement by
|> using (?,?) and place holders for the (2,2).
|
|Why do you call nextval? If your key attribute is "serial", postgres calls 
|nextval for you autiomatically. Thus, you call it once and postgres calls it 
|once, hence an increment of 2
|
|Horst

this is what i want to do:
i want to insert data into a table that has a serialized key. then i want
the value of the key for the row i just inserted. from the stuff i read
on the net i though i was supposed to run the nextval command first then
preform the insert. referring specifically to the url below:

http://www.mail-archive.com/dbi-users(at)isc(dot)org/msg15723.html

i was under the impression that nextval would give what the nextval would
be. i didnt think it was suppose to increment it. after reading your
response and someone else's it seems i need to get the next value of the
key and insert it explicitly is that correct?

for what it's worth the sql to create the table:
CREATE TABLE testtable(
    the_key     serial,
    an_id       int4,
    timestamp   int4,       
    PRIMARY KEY(the_key));



thanks
john


In response to

Responses

pgsql-novice by date

Next:From: Horst HerbDate: 2001-08-05 23:02:55
Subject: Re: nextval/dbi question
Previous:From: Horst HerbDate: 2001-08-05 22:34:06
Subject: Re: nextval/dbi question

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group