Thanks for the reply. Thank you for pointing out Error module and try and catch block. However, I am trying to avoid installing extra module. I found an interesting behavior in my script. In a subroutine, I perform an insert (RaiseError=>0), then i check $dbh->state(Always false if success). If $dbh->state is false (Here I assume failure is caused by key constraint), then I update the record. The interesting part is, if the update statement is within the caller subroutine, the record is updated. If i put the update statement in another subroutine and have the main subroutine to call it, update will not work and no error is returned by update. Here is the code snippet that will not work and work.
Working version =============================================== my $sth=$dbh->prepare(qw {insert_query}); $sth->execute(@bind_param); if($sth->state){ $sth=$dbh->prepare(qw {update_query}); $sth->execute(@bind_param); } $dbh->commit(); ================================================ Bad version ================================================ my $sth=$dbh->prepare(qw {insert_query}); $sth->execute(@bind_param); if($sth->state){ updateSubRoutine($dbh, $param1, $param2); } $dbh->commit(); *********updateSubRoutine********** sub updateSubRoutine{ my ($dbh,$param1, $param2) = @_; my $sth = $dbh->prepare(qw { update_query}); $sth->execute(@params); #$dbh->commit(); also don't work. } *********************************** Can someone give me some suggestions? Thanks. --- "Hanson, Rob" <[EMAIL PROTECTED]> wrote: > Take a look at the Error module (although you can > use eval as well). > > use DBI; > use Error qw(:try); > > my $dbh = DBI->connect(...); > $dbh->{RaiseError} = 1; > > try { > $dbh->do("insert ..."); > } > catch Error with { > print STDERR "Insert failed!"; > } > > $dbh->do("update ..."); > > > ...Or if you only want to update when the insert > fails, then put the insert > in the catch block. > > Rob > > > -----Original Message----- > From: Titu Kim [mailto:[EMAIL PROTECTED] > Sent: Wednesday, August 27, 2003 2:37 PM > To: [EMAIL PROTECTED] > Subject: How to cate DBI error and pass the control > to subroutine? > > > Hi, > I am using DBI and DBD to insert into database. > If > the insert fails because of the key constraints, I > want to continue to update that record. How can I it > do this after I issue $sth->execute() statement? > > > Thanks. > > __________________________________ > Do you Yahoo!? > Yahoo! SiteBuilder - Free, easy-to-use web site > design software > http://sitebuilder.yahoo.com > > -- > To unsubscribe, e-mail: > [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]