从 SQL 服务器的 XML 列中选择字段时,我没有得到任何结果,也没有错误
When selecting fields from XML column in SQL server I get no result and no error
我有一个 table 和一个 XML 类型化列,其中包含一个带有命名空间的 XML 结构。
Table 名为 PersonX
它包含一个名为 customData
的 XML 类型列
customData 列包含此结构:
<personX:CustomData xmlns:personX="http://www.provider.com/PersonX">
<MailingStreet>123 First Street, APT 123</MailingStreet>
<MailingCity>Dallas</MailingCity>
<MailingState>TX</MailingState>
<MailingZip>73425</MailingZip>
<ResidenceCity>Dallas</ResidenceCity>
<ResidenceCounty>Dallas</ResidenceCounty>
<ExportDate>2021-05-19T00:00:00Z</ExportDate>
<IssueDate>2021-05-18T00:00:00Z</IssueDate>
<ExpireDate>2022-05-23T00:00:00Z</ExpireDate>
<SubmissionDate>2021-05-03T00:00:00Z</SubmissionDate>
</personX:CustomData>
我的查询:
declare @xml xml
select @xml = customData from PersonX
;with xmlnamespaces('http://www.provider.com/PersonX' as ns, DEFAULT 'http://www.provider.com/PersonX')
select Person.x.value('MailingStreet[1]','varchar(max)') as street
from @xml.nodes('personX') as Person(x)
当我 运行 时,我得到一个空白的街道值。我认为这与根元素 中的冒号有关
我想我错过了一些简单的东西?
还有一个额外的问题,我如何 运行 table 本身的查询而不是选择变量?
请尝试以下解决方案。
直接来自table,没有临时变量。
不需要定义同一个命名空间两次。
SQL
-- DDL and sample data population, start
DECLARE @tbl table (ID INT IDENTITY PRIMARY KEY, customData XML);
INSERT INTO @tbl ( customData) VALUES
(N'<personX:CustomData xmlns:personX="http://www.provider.com/PersonX">
<MailingStreet>123 First Street, APT 123</MailingStreet>
<MailingCity>Dallas</MailingCity>
<MailingState>TX</MailingState>
<MailingZip>73425</MailingZip>
<ResidenceCity>Dallas</ResidenceCity>
<ResidenceCounty>Dallas</ResidenceCounty>
<ExportDate>2021-05-19T00:00:00Z</ExportDate>
<IssueDate>2021-05-18T00:00:00Z</IssueDate>
<ExpireDate>2022-05-23T00:00:00Z</ExpireDate>
<SubmissionDate>2021-05-03T00:00:00Z</SubmissionDate>
</personX:CustomData>');
-- DDL and sample data population, end
WITH XMLNAMESPACES ('http://www.provider.com/PersonX' AS personX)
SELECT ID
, c.value('(MailingStreet/text())[1]', 'VARCHAR(50)') AS MailingStreet
, c.value('(MailingCity/text())[1]', 'VARCHAR(50)') AS MailingCity
FROM @tbl
CROSS APPLY customData.nodes('/personX:CustomData') AS t(c);
-- if enforced by the XML Schema
WITH XMLNAMESPACES ('http://www.provider.com/PersonX' AS personX)
SELECT ID
, c.value('(MailingStreet)[1]', 'VARCHAR(50)') AS MailingStreet
, c.value('(MailingCity)[1]', 'VARCHAR(50)') AS MailingCity
FROM @tbl
CROSS APPLY customData.nodes('/personX:CustomData') AS t(c);
输出
+----+---------------------------+-------------+
| ID | MailingStreet | MailingCity |
+----+---------------------------+-------------+
| 1 | 123 First Street, APT 123 | Dallas |
+----+---------------------------+-------------+
我有一个 table 和一个 XML 类型化列,其中包含一个带有命名空间的 XML 结构。 Table 名为 PersonX 它包含一个名为 customData
的 XML 类型列customData 列包含此结构:
<personX:CustomData xmlns:personX="http://www.provider.com/PersonX">
<MailingStreet>123 First Street, APT 123</MailingStreet>
<MailingCity>Dallas</MailingCity>
<MailingState>TX</MailingState>
<MailingZip>73425</MailingZip>
<ResidenceCity>Dallas</ResidenceCity>
<ResidenceCounty>Dallas</ResidenceCounty>
<ExportDate>2021-05-19T00:00:00Z</ExportDate>
<IssueDate>2021-05-18T00:00:00Z</IssueDate>
<ExpireDate>2022-05-23T00:00:00Z</ExpireDate>
<SubmissionDate>2021-05-03T00:00:00Z</SubmissionDate>
</personX:CustomData>
我的查询:
declare @xml xml
select @xml = customData from PersonX
;with xmlnamespaces('http://www.provider.com/PersonX' as ns, DEFAULT 'http://www.provider.com/PersonX')
select Person.x.value('MailingStreet[1]','varchar(max)') as street
from @xml.nodes('personX') as Person(x)
当我 运行 时,我得到一个空白的街道值。我认为这与根元素 我想我错过了一些简单的东西? 还有一个额外的问题,我如何 运行 table 本身的查询而不是选择变量?
请尝试以下解决方案。
直接来自table,没有临时变量。
不需要定义同一个命名空间两次。
SQL
-- DDL and sample data population, start
DECLARE @tbl table (ID INT IDENTITY PRIMARY KEY, customData XML);
INSERT INTO @tbl ( customData) VALUES
(N'<personX:CustomData xmlns:personX="http://www.provider.com/PersonX">
<MailingStreet>123 First Street, APT 123</MailingStreet>
<MailingCity>Dallas</MailingCity>
<MailingState>TX</MailingState>
<MailingZip>73425</MailingZip>
<ResidenceCity>Dallas</ResidenceCity>
<ResidenceCounty>Dallas</ResidenceCounty>
<ExportDate>2021-05-19T00:00:00Z</ExportDate>
<IssueDate>2021-05-18T00:00:00Z</IssueDate>
<ExpireDate>2022-05-23T00:00:00Z</ExpireDate>
<SubmissionDate>2021-05-03T00:00:00Z</SubmissionDate>
</personX:CustomData>');
-- DDL and sample data population, end
WITH XMLNAMESPACES ('http://www.provider.com/PersonX' AS personX)
SELECT ID
, c.value('(MailingStreet/text())[1]', 'VARCHAR(50)') AS MailingStreet
, c.value('(MailingCity/text())[1]', 'VARCHAR(50)') AS MailingCity
FROM @tbl
CROSS APPLY customData.nodes('/personX:CustomData') AS t(c);
-- if enforced by the XML Schema
WITH XMLNAMESPACES ('http://www.provider.com/PersonX' AS personX)
SELECT ID
, c.value('(MailingStreet)[1]', 'VARCHAR(50)') AS MailingStreet
, c.value('(MailingCity)[1]', 'VARCHAR(50)') AS MailingCity
FROM @tbl
CROSS APPLY customData.nodes('/personX:CustomData') AS t(c);
输出
+----+---------------------------+-------------+
| ID | MailingStreet | MailingCity |
+----+---------------------------+-------------+
| 1 | 123 First Street, APT 123 | Dallas |
+----+---------------------------+-------------+