Given this XML:
鉴于这种XML:
<Documents>
<Batch BatchID = "1" BatchName = "Fred Flintstone">
<DocCollection>
<Document DocumentID = "269" KeyData = "" />
<Document DocumentID = "6" KeyData = "" />
<Document DocumentID = "299" KeyData = "" ImageFile="Test.TIF" />
</DocCollection>
</Batch>
<Batch BatchID = "2" BatchName = "Barney Rubble">
<DocCollection>
<Document DocumentID = "269" KeyData = "" />
<Document DocumentID = "6" KeyData = "" />
</DocCollection>
</Batch>
</Documents>
I need to insert it into a table in SQL Server in this format:
我需要将它以这种格式插入到SQL Server中的一个表中:
BatchID BatchName DocumentID
1 Fred Flintstone 269
1 Fred Flintstone 6
1 Fred Flintstone 299
2 Barney Rubble 269
2 Barney Rubble 6
This SQL:
这个SQL:
SELECT
XTbl.XCol.value('./@BatchID','int') AS BatchID,
XTbl.XCol.value('./@BatchName','varchar(100)') AS BatchName,
XTbl.XCol.value('DocCollection[1]/DocumentID[1]','int') AS DocumentID
FROM @Data.nodes('/Documents/Batch') AS XTbl(XCol)
gets me this result:
让我这个结果:
BatchID BatchName DocumentID
1 Fred Flintstone NULL
2 Barney Rubble NULL
What am I doing wrong?
我做错了什么?
Also, can someone recommend a good tutorial for XML in SQL Server?
另外,有人能推荐SQL Server中的XML教程吗?
Thanks
谢谢
Carl
卡尔
1 个解决方案
#1
6
You were close.
你是亲密的。
Using a wildcard and a CROSS APPLY, you can generate multiple records.
使用通配符和交叉应用,您可以生成多个记录。
Changed alias to lvl1 and lvl2 to better illustrate.
将别名更改为lvl1和lvl2以更好地说明。
Declare @XML xml = '
<Documents>
<Batch BatchID = "1" BatchName = "Fred Flintstone">
<DocCollection>
<Document DocumentID = "269" KeyData = "" />
<Document DocumentID = "6" KeyData = "" />
<Document DocumentID = "299" KeyData = "" ImageFile="Test.TIF" />
</DocCollection>
</Batch>
<Batch BatchID = "2" BatchName = "Barney Rubble">
<DocCollection>
<Document DocumentID = "269" KeyData = "" />
<Document DocumentID = "6" KeyData = "" />
</DocCollection>
</Batch>
</Documents>
'
Select BatchID = lvl1.n.value('@BatchID','int')
,BatchName = lvl1.n.value('@BatchName','varchar(50)')
,DocumentID = lvl2.n.value('@DocumentID','int')
From @XML.nodes('Documents/Batch') lvl1(n)
Cross Apply lvl1.n.nodes('DocCollection/Document') lvl2(n)
Returns
返回
BatchID BatchName DocumentID
1 Fred Flintstone 269
1 Fred Flintstone 6
1 Fred Flintstone 299
2 Barney Rubble 269
2 Barney Rubble 6
#1
6
You were close.
你是亲密的。
Using a wildcard and a CROSS APPLY, you can generate multiple records.
使用通配符和交叉应用,您可以生成多个记录。
Changed alias to lvl1 and lvl2 to better illustrate.
将别名更改为lvl1和lvl2以更好地说明。
Declare @XML xml = '
<Documents>
<Batch BatchID = "1" BatchName = "Fred Flintstone">
<DocCollection>
<Document DocumentID = "269" KeyData = "" />
<Document DocumentID = "6" KeyData = "" />
<Document DocumentID = "299" KeyData = "" ImageFile="Test.TIF" />
</DocCollection>
</Batch>
<Batch BatchID = "2" BatchName = "Barney Rubble">
<DocCollection>
<Document DocumentID = "269" KeyData = "" />
<Document DocumentID = "6" KeyData = "" />
</DocCollection>
</Batch>
</Documents>
'
Select BatchID = lvl1.n.value('@BatchID','int')
,BatchName = lvl1.n.value('@BatchName','varchar(50)')
,DocumentID = lvl2.n.value('@DocumentID','int')
From @XML.nodes('Documents/Batch') lvl1(n)
Cross Apply lvl1.n.nodes('DocCollection/Document') lvl2(n)
Returns
返回
BatchID BatchName DocumentID
1 Fred Flintstone 269
1 Fred Flintstone 6
1 Fred Flintstone 299
2 Barney Rubble 269
2 Barney Rubble 6