Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: getting the ids of records affected by an input statement

by JavaFan (Canon)
on Nov 22, 2010 at 19:16 UTC ( [id://873024]=note: print w/replies, xml ) Need Help??


in reply to getting the ids of records affected by an input statement

$dbh -> begin_work; my $ids = $dbh -> selectall_arrayref("SELECT id FROM table WHERE col +1 != ? AND col2 = ?", undef, $val1, $val2); $dbh -> do("update table set col1= ? where col2 = ?", undef, $val1, +$val2); $dbh -> commit;
Doing error checking (and rolling back if necessary) is left as an exercise for the reader.

Replies are listed 'Best First'.
Re^2: getting the ids of records affected by an input statement
by Anonymous Monk on Nov 22, 2010 at 23:38 UTC
    Thanks for your reply. I can see that a 'workaround' is to do the select and find out the ids that would be affected and then do the updae. Why do you need a transaction though?
      Why do you need a transaction though?
      1. You're modifying the database. Not putting a database modification inside a transaction is far more unforgivable than not ever using strict, warnings or checking the return values of system calls. In fact, if it were my call, not putting database modifications inside transactions would put you on the fast track to getting fired.
      2. You do not want the state (or to be precise, your view) of the database to actually change between the select and the update. Hence the transaction. (I'm assuming you have a proper default isolation level active).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://873024]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-16 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found