XML 文書の部分更新
XML 列の XML 文書の一部を更新するには、組み込みスカラー関数 XMLMODIFY を指定した SQL UPDATE ステートメントを使用します。
XMLMODIFY 関数には、ノードの挿入、ノードの削除、ノードの置換、または XML 列に格納されている XML 文書のノード値の置換に使用できる基本更新式が指定されます。
XMLMODIFY を使用して XML 文書の一部を更新するには、この XML 文書が入っている列で XML バージョンがサポートされている必要があります。
基本更新式のタイプを以下に示します。
- 挿入式
- 1 つ以上のノードのコピーを、ノード・シーケンス内の指定の位置に挿入します。
- 置換式
- 既存のノードを、0 または 1 つ以上のノードからなる新しいシーケンスに置換するか、またはノードの ID を維持した状態でノードの値を置換します。
- 削除式
- 0 または 1 つ以上のノードをノード・シーケンスから削除します。
例
注文書 ID (POID) が 5000 の purchaseOrder 文書の 2 番目の shipTo ノードを置換するとします。この文書は次のようになります。
<ipo:purchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ipo="http://www.example.com/IPO"
orderDate="2008-12-01">
<shipTo exportCode="1" xsi:type="ipo:UKAddress">
<name>Helen Zoe</name>
<street>55 Eden Street</street>
<city>San Jose</city>
<state>CA</state>
<postcode>CB1 1JR</postcode>
</shipTo>
<shipTo exportCode="1" xsi:type="ipo:UKAddress">
<name>Joe Lee</name>
<street>66 University Avenue</street>
<city>Palo Alto</city>
<state>CA</state>
<postcode>CB1 1JR</postcode>
</shipTo>
<billTo xsi:type="ipo:USAddress">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<items>
<item partNum="833-AA">
<productName>Lapis necklace</productName>
<quantity>1</quantity>
<USPrice>99.95</USPrice>
<ipo:comment>Want this for the holidays!</ipo:comment>
<shipDate>2008-12-05</shipDate>
</item>
<item partNum="945-ZG">
<productName>Sapphire Bracelet</productName>
<quantity>2</quantity>
<USPrice>178.99</USPrice>
<shipDate>2009-01-03</shipDate>
</item>
</items>
</ipo:purchaseOrder>
shipTo ノードを置換するには、次に示すような SQL UPDATE ステートメントを使用できます。
UPDATE PURCHASEORDER
SET INFO = XMLMODIFY(
'declare namespace ipo="http://www.example.com/IPO";
replace node /ipo:purchaseOrder/shipTo[name="Joe Lee"]
with $x', XMLPARSE(
'<shipTo exportCode="1" xsi:type="ipo:USAddress">
<name>Joe Lee</name>
<street>555 Quarry Road</street>
<city>Palo Alto</city>
<state/>CA
<postcode>94304</postcode>
</shipTo>') AS "x")
WHERE POID=5000
このステートメントの実行後、PORDER 列の文書の内容は次のようになります。
<ipo:purchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ipo="http://www.example.com/IPO"
orderDate="2008-12-01">
<shipTo exportCode="1" xsi:type="ipo:UKAddress">
<name>Helen Zoe</name>
<street>55 Eden Street</street>
<city>San Jose</city>
<state>CA</state>
<postcode>CB1 1JR</postcode>
</shipTo>
<shipTo exportCode="1" xsi:type="ipo:USAddress">
<name>Joe Lee</name>
<street>555 Quarry Road</street>
<city>Palo Alto</city>
<state>CA</state>
<postcode>94304</postcode>
</shipTo>
<billTo xsi:type="ipo:USAddress">
<name>Robert Smith</name>
<street>505 First Street</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<items>
<item partNum="833-AA">
<productName>Lapis necklace</productName>
<quantity>1</quantity>
<USPrice>99.95</USPrice>
<ipo:comment>Want this for the holidays!</ipo:comment>
<shipDate>2008-12-05</shipDate>
</item>
<item partNum="945-ZG">
<productName>Sapphire Bracelet</productName>
<quantity>2</quantity>
<USPrice>178.99</USPrice>
<shipDate>2009-01-03</shipDate>
</item>
</items>
</ipo:purchaseOrder>