<?xml version="1.0" encoding="windows-1252"?>
<node id="302181" title="How to get a placeholder to keep the current column value?" created="2003-10-26 04:23:27" updated="2005-07-27 17:15:06">
<type id="115">
perlquestion</type>
<author id="272239">
liz</author>
<data>
<field name="doctext">
While I'm (finally) getting to grips with using DBI placeholders, I realise that there may be one situation where placeholders are not the solution.
&lt;P&gt;
The situation: I'm writing an XML to RDBMS Perl layer that should allow my customer to use XML to update records in a database.  For each type of information (e.g. address information), there are a number of expected nodes in the XML that correspond to columns in a table in the database.  For example, a complete description of a record would be:
&lt;code&gt;
&lt;address id="34567"&gt;
 &lt;phone&gt;123&lt;/phone&gt;
 &lt;street&gt;Fred Ziffel Avenue 42&lt;/street&gt;
 &lt;zip&gt;123456789&lt;/zip&gt;
 &lt;city&gt;Hooterville&lt;/city&gt;
&lt;/address&gt;
&lt;/code&gt;
&lt;P&gt;
Now, when the customer supplies the following XML:
&lt;code&gt;
&lt;address id="34567" mode="update"&gt;
 &lt;phone&gt;456&lt;/phone&gt;
 &lt;!-- note the absence of other nodes --&gt;
&lt;/address&gt;
&lt;/code&gt;
&lt;P&gt;
I want it to update just the "phone" column for that record in the database.
&lt;P&gt;
Now, at least in MySQL you can put the following in the SQL to indicate you don't want to change the value of a column.  For example: the "street", "zip" and "city" fields.:
&lt;code&gt;
UPDATE address SET
 phone="456",
 street=street,
 zip=zip,
 city=city
WHERE ID="34567"
&lt;/code&gt;
&lt;P&gt;
so in principle this should allow you to do a &lt;code&gt;prepare&lt;/code&gt; once because the query would always be the same.  Only those fields that you don't want changed, get their field name replaced rather than an actual value.
&lt;P&gt;
However, it seems to me that it is not possible to do this using placeholders, as you can only specify &lt;B&gt;values&lt;/B&gt; with placeholders, rather than a &lt;B&gt;name&lt;/B&gt; of the column.
&lt;P&gt;
Of course, I could always fetch the old values from the record in the database first, but I feel that sort of defeats the purpose.  Or would it?
&lt;P&gt;
Anyway, my question: can I use placeholders in case I don't want to change the value of a column, without obtaining the old value from the record first?
&lt;P&gt;
Liz
&lt;P&gt;
&lt;B&gt;Update&lt;/B&gt;:&lt;BR&gt;
Thanks to [runrig] and [pg]: it would seem that "Dynamic SQL" (never knew there was a phrase for it, as it is the way I was always used to doing it using the older Mysql.pm modules) &lt;B&gt;is&lt;/B&gt; the way to go for me, as the number of different field combinations would provide me with a combinatorial explosion.  I was hoping that I could use prepare() for this, but it seems that that just isn't possible.
&lt;P&gt;
&lt;B&gt;Good News&lt;/B&gt;:&lt;BR&gt;
[Jenda]++ shows in [id://302267] that there _is_ a way to do this with a pre-prepared statement handle, even in MySQL!</field>
</data>
</node>
