Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old June 30th, 2004, 02:51 PM
mrgreenfur mrgreenfur is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 18 mrgreenfur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Perl DBI:Oracle last inserted id

I'm coding a web application in perl and oracle. Whats the best way to get the id of a row just inserted? The DBI function last_insert_id seems great, but I can't make it work or find any examples online.... anyone have experience?

Here's the man page for last_insert_id:

http://search.cpan.org/~timb/DBI/DBI.pm#last_insert_id

thanks!

Reply With Quote
  #2  
Old June 30th, 2004, 03:53 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
That's for autoincrement fields in Access and the like.

If the table has a sequence or a unique key that counts upward and the key column is indexed try -

Code:
SELECT ROWID 
from mytable 
where mytable.unique_key in
   (select 
    max(mytable.unique_key) 
    from mytable); 

I'm assuming your idea of an id is ROWID.

Reply With Quote
  #3  
Old June 30th, 2004, 04:15 PM
mrgreenfur mrgreenfur is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 18 mrgreenfur User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Aha, the dbi doc alludes to giveing the method the sequence as part of the \%attr argument. Since oracle uses triggers and sequences for incrementing primary keys/id's I thought maybe it could work.

Selecting the max id could always work, presuming no new rows have been added in between the queries.

Thanks!

Reply With Quote
  #4  
Old July 4th, 2004, 01:22 AM
swstephe swstephe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Location: Brunei, Borneo Island, South East Asia
Posts: 9 swstephe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to swstephe
assume you "create table foo(id integer)" and "create sequence foo_seq"
...
use DBI qw(:sql_types);
my $dbh = DBI->connect('dbi:Oracle:','scott','tiger') || die $DBI::errstr;
$sth = $dbh->prepare(
'insert into foo(id) values (foo_seq.nextval) returning id into ?')
|| die $DBI::Errstr;
my $new_id;
$sth->bind_param_inout(1,\$new_id,38);
$sth->execute() || die $DBI::errstr;
print "new id = $new_id";
$sth->finish;
$dbh->disconnect;
---
cool eh? works in Oracle 9i and above. The "RETURNING INTO " places whatever set of values you just inserted into your bind variables.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Perl DBI:Oracle last inserted id


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway