2
我需要有一個腳本可以插入/新的XML子節點追加到一個預先存在的XML父節點。SQL Server中的XML子節點追加到父節點
--New child nodes
DECLARE @XMLChildData XML
SET @XMLChildData = '
<Persons>
<Person>
<Firstname>Gary</Firstname>
<Surname>Smith</Surname>
<Telephone>0115547899</Telephone>
<Address>
<AddressLine>1 Church Lane</AddressLine>
<AddressLine>Rosebank</AddressLine>
<AddressLine>Houghton</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Wayne</Firstname>
<Surname>Farmey</Surname>
<Telephone>0117453269</Telephone>
<Address>
<AddressLine>51 Oak Street</AddressLine>
<AddressLine>Rivionia</AddressLine>
<AddressLine>Sandton</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Mark</Firstname>
<Surname>Jones</Surname>
<Telephone>0119854741</Telephone>
<Address>
<AddressLine>4 Arum Lane</AddressLine>
<AddressLine>Glen Hazel</AddressLine>
<AddressLine>Johannesburg</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
</Persons>'
--Existing parent node
DECLARE @XMLParentData XML
SET @XMLParentData = '
<Persons>
<Person>
<Firstname>Sarah</Firstname>
<Surname>Gray</Surname>
<Telephone>0113265874</Telephone>
<Address>
<AddressLine>78 Emerl Aveune</AddressLine>
<AddressLine>Fourways</AddressLine>
<AddressLine>Sandton</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Jenna</Firstname>
<Surname>Reed</Surname>
<Telephone>0114781102</Telephone>
<Address>
<AddressLine>6 Park Lane</AddressLine>
<AddressLine>Parkhurst</AddressLine>
<AddressLine>Rosebank</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Mike</Firstname>
<Surname>Wilke</Surname>
<Telephone>0116532003</Telephone>
<Address>
<AddressLine>22 High Road</AddressLine>
<AddressLine>Modderfontein</AddressLine>
<AddressLine>Edenvale</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
</Persons>'
我想最終的結果是:
<Persons>
<Person>
<Firstname>Sarah</Firstname>
<Surname>Gray</Surname>
<Telephone>0113265874</Telephone>
<Address>
<AddressLine>78 Emerl Aveune</AddressLine>
<AddressLine>Fourways</AddressLine>
<AddressLine>Sandton</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Jenna</Firstname>
<Surname>Reed</Surname>
<Telephone>0114781102</Telephone>
<Address>
<AddressLine>6 Park Lane</AddressLine>
<AddressLine>Parkhurst</AddressLine>
<AddressLine>Rosebank</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Mike</Firstname>
<Surname>Wilke</Surname>
<Telephone>0116532003</Telephone>
<Address>
<AddressLine>22 High Road</AddressLine>
<AddressLine>Modderfontein</AddressLine>
<AddressLine>Edenvale</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Gary</Firstname>
<Surname>Smith</Surname>
<Telephone>0115547899</Telephone>
<Address>
<AddressLine>1 Church Lane</AddressLine>
<AddressLine>Rosebank</AddressLine>
<AddressLine>Houghton</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Wayne</Firstname>
<Surname>Farmey</Surname>
<Telephone>0117453269</Telephone>
<Address>
<AddressLine>51 Oak Street</AddressLine>
<AddressLine>Rivionia</AddressLine>
<AddressLine>Sandton</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
<Person>
<Firstname>Mark</Firstname>
<Surname>Jones</Surname>
<Telephone>0119854741</Telephone>
<Address>
<AddressLine>4 Arum Lane</AddressLine>
<AddressLine>Glen Hazel</AddressLine>
<AddressLine>Johannesburg</AddressLine>
<AddressLine>South Africa</AddressLine>
</Address>
</Person>
</Persons>
我知道我需要使用.modify(),但我不知道如何通過子節點進行迭代,並插入/追加每個小孩"<person>"
節點放入父節點"<persons>"
。
我認爲這將需要類似的東西如下
SET @XMLParentData.modify('
insert
(
sql:variable("@XMLChildData")
)
after
(/Person[1]/Person[1])
')
SELECT @XMLData
感謝的Mikael,如果我把你的第一個代碼片斷我有同樣的問題與建議Bummi,是我似乎從附加子節點的列表到父列表。我希望只有父節點xml,其根節點爲,並且只將子節點xml中的節點附加到父節點xml中。如果我把你的第二個代碼片段按預期工作。我怎樣才能達到你的第一個代碼片段所需的結果。我想了解我做錯了什麼,所以我可能會更好。 –
user3917837
2014-10-21 07:03:57
@ user3917837第一個按預期工作。您可能忘記更改您在'sql:variable'中使用的變量。看看這個[SQL Fiddle](http://sqlfiddle.com/#!3/d41d8/40302)。 – 2014-10-21 07:30:41
對不起......你是對的。謝謝Mikael – user3917837 2014-10-21 07:37:34