Perobl has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks
I need to obtain the id of the last record inserted in my table (SQL database). I believe I need to use SCOPE_IDENTITY().
My code looks like this:
my $dbh = DBI -> connect("$dsn; server=$host; Database=$database", $us +er, $auth, \%attr) || die "database connection not made: $DBI::errstr +"; my $sth = $dbh -> prepare("INSERT INTO ocr_main (project_number, lead_ +order, employee_id, due_date, ocr_type, line_item, mech_rnr, mech_rfd +, mech_rfa, mech_dre, ctrl_rnr, ctrl_rfd, ctrl_rfa, ctrl_dre) VALUES +(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") || die "unable to prepar +e query"; $sth -> execute($project_number, $lead_order, $employee_id, $due_date, + $ocr_type, $line_item, $mech_rnr, $mech_rfd, $mech_rfa, $mech_dre, $ +ctrl_rnr, $ctrl_rfd, $ctrl_rfa, $ctrl_dre); $sth -> finish(); $dbh -> disconnect();
I'd like to save the last inserted id in the variable 'id'. This is very straight forward in mySQL, but I'm having difficulty doing it in SQL. Can someone show me the proper syntax please? Thank you so much.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Correct Perl Syntax for Last Record Insert in MS SQL?
by Anonymous Monk on Nov 01, 2011 at 22:33 UTC | |
Re: Correct Perl Syntax for Last Record Insert in MS SQL?
by Anonymous Monk on Nov 01, 2011 at 22:34 UTC | |
Re: Correct Perl Syntax for Last Record Insert in MS SQL?
by Plankton (Vicar) on Nov 01, 2011 at 22:35 UTC | |
Re: Correct Perl Syntax for Last Record Insert in MS SQL?
by Perobl (Beadle) on Nov 02, 2011 at 17:27 UTC | |
by Anonymous Monk on Oct 16, 2015 at 20:18 UTC |
Back to
Seekers of Perl Wisdom