Tunisian made Blogging hallucinations
The best solution to catch that mysterious serial

I have been using postgresql for a while, and using serial (autoincrement) is one of must use in many tables (about 70%), sometimes we need the serial to create a logical id that should enumerate the records, or juste to get a virtual id non-related with the data, sometimes we need to get back that record instantly inserted into the table, so we have to get get the select currentval(‘seq_name’), then to insert it manually into the table, oh , I know tha’s really jerk, and you can’t claim it after inserting, because in a multi-user/connection world, that last serial would be lost and you’ll certainly hit false values , so …I have finally found the best solutions that fits all my problem :
insert into test values(default);SELECT id FROM test ORDER BY id DESC LIMIT 1
When executing that query in the same line you’ll get the auto serial id beeing insreterd withing the insert query and that’s all folks !
| Print article | This entry was posted by karim2k on February 8, 2005 at 4:44 pm, and is filed under Uncategorized. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
Comments are closed.