[英]Get XML column from XML in SQL Server
I need to get table with Address
XML column from XML. 我需要从XML获取带有
Address
XML列的表。
Here is what I do: 这是我做的:
DECLARE @Person xml='<Person PersonID="a1fee068-aad4-459b-8ea7-063975f17bed">
<Address State="1" District="15" House="test" Additional="1" />
<Address State="8" District="11" Region="12" Neighborhood="13" Block="14" House="75" Additional="somewhere" />
</Person>'
DECLARE @handle INT
DECLARE @PrepareXmlStatus INT
EXEC @PrepareXmlStatus= sp_xml_preparedocument @handle OUTPUT, @Person
SELECT *
FROM
OPENXML(@handle, '/Person', 2)
WITH (
Address xml
)
EXEC sp_xml_removedocument @handle
The result is only first Address
from the XML. 结果只是XML中的第一个
Address
。 I can't understand why.... 我不明白为什么....
Any suggestions how get all records with Address
tag? 有关如何使用
Address
标记获取所有记录的任何建议
You can use a XML query like this: 您可以使用这样的XML查询:
DECLARE @Person xml=' <Person PersonID="a1fee068-aad4-459b-8ea7-063975f17bed"> <Address State="1" District="15" House="test" Additional="1" /> <Address State="8" District="11" Region="12" Neighborhood="13" Block="14" House="75" Additional="somewhere" /> </Person>' SELECT data.value('./@State', 'int') as State, data.value('./@District', 'int') as District, data.value('./@House', 'nvarchar(50)') as House, data.value('./@Additional', 'nvarchar(50)') as Additional, data.value('./@Region', 'int') as Region, data.value('./@Neighborhood', 'int') as Neighborhood, data.value('./@Block', 'int') as Block FROM @Person.nodes('/Person/Address') AS t1(data);
\nState |国家| District |
区| House |
房子| Additional |
附加| Region |
地区| Neighborhood |
邻里| Block
块\n----: |
----:| -------: |
-------:| :---- |
:---- | :--------- |
:--------- | -----: |
-----:| -----------: |
-----------:| ----:
----:\n 1 |
1 | 15 |
15 | test |
测试| 1 |
1 | null |
null | null |
null | null
空值\n 8 |
8 | 11 |
11 | 75 |
75 | somewhere |
某处| 12 |
12 | 13 |
13 | 14
14\n\n
If you want each Address block: 如果你想要每个地址块:
\n SELECT data.query('.')SELECT data.query('。')\n FROM @Person.nodes('/Person/Address') AS t1(data)
FROM @ Person.nodes('/ Person / Address')AS t1(数据)\n GO
走\n\n|
| (No column name) |
(没有列名)|\n|
| :------------------------------------------------------------------------------------------------------------- |
:------------------------------------------------- -------------------------------------------------- ---------- |\n|
| <Address State="1" District="15" House="test" Additional="1" /> |
<地址状态=“1”区=“15”House =“test”附加=“1”/> |\n|
| <Address State="8" District="11" Region="12" Neighborhood="13" Block="14" House="75" Additional="somewhere" /> |
<Address State =“8”District =“11”Region =“12”Neighborhood =“13”Block =“14”House =“75”Additional =“somewhere”/> |\n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.