develooper Front page | perl.dbi.dev | Postings from August 2002

RE: Multiple Result sets in DBD::ODBC (and others?)

Thread Previous | Thread Next
From:
John_Tobey
Date:
August 22, 2002 12:39
Subject:
RE: Multiple Result sets in DBD::ODBC (and others?)
Message ID:
85256C1D.006BCAD2.00@notes.intdata.com


>> I have never yet needed to know how many rows were affected
>> by any insert/update, and I don't expect I ever will, unless
>> I were to see a good example of when it's really useful.
>>
>>   -- Joe
>>
>
>Here's an example of where it is useful (not that there
>aren't other good ways to do this ).
>
>I have a table with primary key K, and other
>columns  C1,...,Cn.
>
>I have the tuple (k,c1,...,cn) that I want to add to
>the table, i.e if the key k already exists in the table
>I want to update the row, otherwise I want to add the
>new row.
>
>Here's how to do it:
>
>    $my @parms = (c1,...,cn,k);
>    $rows = $db->do("update table set C1=? .... CN=?
>                   where K=?", undef, @parms);
>      if ($rows == 0) {
>        # i guess key wasn't there
>        $db->do("insert into table (C1,...,Cn,K)
>              values (?,...,?,?)", @parms);
>    }
>
>otherwise I have to do a select first, and then
>an update or an insert, or do the insert and
>watch for an error and then go ahead and do the
>update -- a little more complicated.

Also error-prone, if your transaction isolation level
allows someone to insert the key between your check and
your insert.

I have used this trick with the row count, too.

-John



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About