On 4 May 2001, at 14:46, traja wrote:

> Hi,
> 
> I have 2 insert statements which insert data into two different
> tables. These 2 inserts need to be run atomically, How can I do this?
> If I commit at the end of the two inserts, would that work?

If your rdbms supports it and you have autocommit off,

Page 159 of "Programming the Perl DBI" has the code (and all 
sorts of other useful info easily accessible).

Here's a snippet from one of my test scripts, essentially I put the 
dbi work in the eval statement. The Perl DBI book has a lot more 
info on this.  

eval {
        my $sql = qq|INSERT INTO company_profile 
(company_description,company_name,culture,employees,employer
_id,growth_rate,leadership,mission,sec_fillings_url,type,url,year) 
VALUES(?,?,?,?,?,?,?,?,?,?,?,?)|;
        my @vals = qw(sadfds 007_company_name sadfsadfsa 500 47 
91-100% sadfsad afsadf sdfdsa established sadfsadf 1965);
        my $sth;
        $sth= $dbh->prepare($sql);
        $sth->execute(@vals);
        
        $sql = qq|UPDATE users set username = 'NOT' where f_id = ?|;
        $sth= $dbh->prepare($sql);
        $sth->execute('49');
        
        $sth->finish();
        $dbh->commit;
};

if($@) {
        $dbh->rollback; 
        print qq|Cry me to sleep: $@<hr>|;
}


---------------------------
"Reality is that which, when you stop believing in it, doesn't go
away".
                -- Philip K. Dick

Reply via email to