2

私はこのアプローチを使用しています。SQL にエラーがある場合、ロールバックは asset_group の最初の ID に対してのみ発生します。残りの ID は無視されます。私はそれを正しい方法でやっていますか?

my $sql = "sql batch that update and insert depending on the condition";  
$dbh->{RaiseError} = 1;  
$dbh->{PrintError} = 0;  
$dbh->{AutoCommit} = 0;  

my $sth = $dbh->prepare($sql);  
my @error = ();  
my $num = 0;  
foreach my $id (@asset_group) {  
 next if ($id eq '');  
 eval {  
  $sth->bind_param(1, $id);  
  $sth->bind_param(2, $vars{'other_id'});  
  $sth->execute();  

 };  
 if ($@) {  
  $dbh->rollback();  
  push @error, $@  
 } else {  
  $dbh->commit();  
 }  
}
4

1 に答える 1

7

データベースによっては、変更を開始する前にbegin workを発行する必要がある場合があります。Informix がそれを要求したことを覚えているようです。

また、各実行後にコミットまたはロールバックを発行しているようです。一度コミットすると、ロールバックできません。通常、次のようなことを言います

$dbh->begin_work;
eval {
    for my $id (@asset_group) {  
        next if ($id eq '');  
        $sth->execute($id, $vars{other_id});  
    }
    1; #if it doesn't die then this will force it to return true
} or do {
    my $error = DBI->errstr;
    $dbh->rollback();
    die "could not insert rows: $error\n"
};
$dbh->commit();

を使用しないことに注意してください$@信頼できませ$@ん。

于 2010-09-10T16:49:10.403 に答える