我有三個表table1, table2, table3
與col1, col2
和身份ID
列。這些表關係在數據庫中定義。迭代通過XML SQL INSERT
我試圖創建一個接受XML字符串輸入一個存儲過程,並保存數據到表中。
這是XML輸入
<root>
<table1 col1='a' col2='b'>
<table2Array>
<table2 col1='c' col2='d'>
<table3array>
<table3 col1='g' col2='h' />
<table3 col1='i' col2='j' />
</table3array>
</table2>
<table2 col1='c' col2='d'>
<table3array>
<table3 col1='k' col2='l' />
<table3 col1='i' col2='j' />
</table3array>
</table2>
</table2Array>
</table1>
<table1 col1='a' col2='b'>
<table2Array>
<table2 col1='e' col2='f'>
<table3array>
<table3 col1='i' col2='j' />
<table3 col1='i' col2='j' />
</table3array>
</table2>
<table2 col1='e' col2='f'>
<table3array>
<table3 col1='g' col2='h' />
<table3 col1='g' col2='h' />
</table3array>
</table2>
</table2Array>
</table1>
</root>
此XML從第三方對象來了,我們就不用管了修改第三方對象發出不同格式的XML。
算法:
- 循環每一個節點
- 插入節點屬性到表
- 獲取最後一個標識值與去年的標識值
- 呼叫子節點作爲外鍵
- 做,直到不更多的子節點
是個處理這種情況的唯一方法是什麼?如果是的話如何通過XML節點迭代?
請幫忙!!
感謝,
也先
要回答通過XML節點進行迭代,你可以使用[**'XmlTextReader' **](http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx)從文件/流中讀取,[**'XmlNodeReader' **](http:// msdn.microsoft.com/en-us/library/system.xml.xmlnodereader.aspx)從「XmlElement」實例讀取。我建議通過嘗試在SQL Server中處理XML來做到這一點。 – mellamokb
你使用什麼版本的SQL Server? –
Sql server 2008.我剛發佈了我自己的答案。如果您有不同的處理方法,請發佈您的解決方案。感謝 – Esen