简体   繁体   English

如何从 SQL 服务器中的 XML 列获取数据?

[英]How to get data from XML column in SQL Server?

I have a XML column in a SQL Server database.我在 SQL 服务器数据库中有一个XML列。 I want to get results from that column.我想从该列中获得结果。 Here is what the XML column looks like:这是 XML 列的样子:

<ArrayOfTarget xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/TriTech.InformRMS.Domain.Core.ComplexTypes">
  <Target>
    <AgencyId i:nil="true" />
    <AgencyName i:nil="true" />
    <Id i:nil="true" />
    <Name>Quick Search</Name>
    <Type>Search</Type>
  </Target>
  <Target>
    <AgencyId i:nil="true" />
    <AgencyName i:nil="true" />
    <Id i:nil="true" />
    <Name>Quick Search = wbinc2000125</Name>
    <Type>Quick Search</Type>
  </Target>
  <Target>
    <AgencyId i:nil="true" />
    <AgencyName i:nil="true" />
    <Id i:nil="true" />
    <Name>Results (0)</Name>
    <Type>Result</Type>
  </Target>
</ArrayOfTarget>

Here are some things I have tried but no results:以下是我尝试过但没有结果的一些事情:

select 
    [XML].value ('(ArrayofTarget/Target/Name/node())[1]', 'nvarchar(max)') as Results
from 
    [DB].[dbo].[Table]

Another example:另一个例子:

;WITH XMLNAMESPACES (N'http://www.w3.org/2001/XMLSchema-instance' as X)
SELECT
    [XML].value('(/X:ArrayOfTarget/X:Target/X:Name[1]', 'nvarchar(max)') as Name
FROM [DB].[dbo].[Table]

Here is a working example.这是一个工作示例。

It shows how to handle properly a default namespace as well as how to use two XQuery methods: .nodes() and .value()它展示了如何正确处理默认名称空间以及如何使用两个 XQuery 方法: .nodes().value()

SQL SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO @tbl (xmldata) VALUES
(N'<ArrayOfTarget xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
               xmlns="http://schemas.datacontract.org/2004/07/TriTech.InformRMS.Domain.Core.ComplexTypes">
    <Target>
        <AgencyId i:nil="true"/>
        <AgencyName i:nil="true"/>
        <Id i:nil="true"/>
        <Name>Quick Search</Name>
        <Type>Search</Type>
    </Target>
    <Target>
        <AgencyId i:nil="true"/>
        <AgencyName i:nil="true"/>
        <Id i:nil="true"/>
        <Name>Quick Search = wbinc2000125</Name>
        <Type>Quick Search</Type>
    </Target>
    <Target>
        <AgencyId i:nil="true"/>
        <AgencyName i:nil="true"/>
        <Id i:nil="true"/>
        <Name>Results (0)</Name>
        <Type>Result</Type>
    </Target>
</ArrayOfTarget>');
-- DDL and sample data population, end

;WITH XMLNAMESPACES (DEFAULT 'http://schemas.datacontract.org/2004/07/TriTech.InformRMS.Domain.Core.ComplexTypes')
SELECT c.value('(Name/text())[1]', 'VARCHAR(30)') AS [Name]
    , c.value('(Type/text())[1]', 'VARCHAR(30)') AS [Type]
FROM @tbl CROSS APPLY xmldata.nodes('/ArrayOfTarget/Target') AS t(c);

Output Output

+-----------------------------+--------------+
|            Name             |     Type     |
+-----------------------------+--------------+
| Quick Search                | Search       |
| Quick Search = wbinc2000125 | Quick Search |
| Results (0)                 | Result       |
+-----------------------------+--------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM