我想成功查询(在SSMS中)我的数据库中的一列,该列包含XML中不同类型的所谓AddressElements。包含所有AddressElements的表格如下所示:
create table AddressElements(Address.Id int, AddressType.Id int, AddressElement xml)
AddressElement xml-column中的XML示例结构如下:
<AddressElements xmlns="http://www.myorganisation.nl/schemas/2007/BusinessPropertySchema/AddressElements/Postadres">
<Postadres_default Straatnaam="Main Street" Huisnummer="1" Postcode="9999 XX" Plaats="ROTTERDAM" />
</AddressElements>
我首先尝试使用WHERE语句进行以下查询,以过滤掉我感兴趣的特定AddressElement。
SELECT m.c.value('@Straatnaam', 'nvarchar(50)') AS Straatnaam
, m.c.value('@Huisnummer', 'int') AS Huisnummer
, m.c.value('@Postcode', 'nvarchar(50)') AS Postcode
, m.c.value('@Plaats', 'nvarchar(50)') AS Plaats
FROM Address as s
outer apply s.AddressElements.nodes('AddressElements/Postadres_default') AS m(c)
WHERE s.AddressTypeId = '00000000-0000-0000-0000-000000000002'
不幸的是结果是:
Straatnaam Huisnummer Postcode Plaats
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
我已经搜索过与我类似的问题/帖子,但到目前为止,我无法让这些解决方案适用于我的案例。如果你们中的任何人可以帮助我,那就太好了!非常感谢你提前!
亲切的问候,英格玛
答案 0 :(得分:0)
ZLK评论中提供的解决方案应该有效。
例如,试试这个:
CREATE TABLE Address (AddressId int, AddressTypeId uniqueidentifier, AddressElements xml)
INSERT INTO dbo.Address (AddressId, AddressTypeId, AddressElements)
VALUES (1,'00000000-0000-0000-0000-000000000002',
'<AddressElements xmlns="http://www.myorganisation.nl/schemas/2007/BusinessPropertySchema/AddressElements/Postadres">
<Postadres_default Straatnaam="Main Street" Huisnummer="1" Postcode="9999 XX" Plaats="ROTTERDAM" />
</AddressElements>')
SELECT m.c.value('@Straatnaam', 'nvarchar(50)') AS Straatnaam
, m.c.value('@Huisnummer', 'int') AS Huisnummer
, m.c.value('@Postcode', 'nvarchar(50)') AS Postcode
, m.c.value('@Plaats', 'nvarchar(50)') AS Plaats
FROM Address as s
outer apply s.AddressElements.nodes('*:AddressElements/*:Postadres_default') AS m(c)
WHERE s.AddressTypeId = '00000000-0000-0000-0000-000000000002'
;WITH XMLNAMESPACES('http://www.myorganisation.nl/schemas/2007/BusinessPropertySchema/AddressElements/Postadres' as s)
SELECT m.c.value('@Straatnaam', 'nvarchar(50)') AS Straatnaam
, m.c.value('@Huisnummer', 'int') AS Huisnummer
, m.c.value('@Postcode', 'nvarchar(50)') AS Postcode
, m.c.value('@Plaats', 'nvarchar(50)') AS Plaats
FROM Address as s
outer apply s.AddressElements.nodes('s:AddressElements/s:Postadres_default') AS m(c)
WHERE s.AddressTypeId = '00000000-0000-0000-0000-000000000002'