2

我有一个表作为 id、createdate、outputxml 作为列,其中 outputxml 是一个 XML 数据类型列,其信息如下

<storage xmlns="http://google.com " created-on="2012-12-29">
    <country>India</country>
    <state>tn</point>
    <period type="day" from="2012-12-27" to="2012-12-28" />
    <capacity-total mwh="12898463" mcm="1229.02" country-percentage="6.1%" />
    <net mwh="28004.00" mcm="2.66" />
</storage>

我需要输出为:

id, createdate, storage, created-on, 国家状态, 期间类型, 从, 到, 容量-总 mwh, 容量-总 mwh, 国家-百分比

1 20-01-2012 http://google.com 2012-12-29 abc tn day 2012-12-27 2012-12-28 12898463 1229.02 6.1%

我尝试使用

 select X.N.value(N'(storage/@country)[1]', 'nvarchar(max)'),
        X.N.value(N'(storage/@sso)[2]', 'nvarchar(max)')
   from icissso..ssodataoutput as T
  cross apply T.outputxml.nodes(N'/storage/xmlns') as X(N)

但它没有返回任何值。所以请提出一个解决方案。

4

1 回答 1

2

您需要定义XML 命名空间并在访问值时使用它:

DECLARE @T TABLE (X XML);
INSERT @T VALUES ('<storage xmlns="http://google.com" created-on="2012-12-29">
                    <country>India</country>
                    <state>tn</state>
                    <period type="day" from="2012-12-27" to="2012-12-28" />
                    <capacity-total mwh="12898463" mcm="1229.02" country-percentage="6.1%" />
                    <net mwh="28004.00" mcm="2.66" />
                </storage>');

WITH XMLNAMESPACES ('http://google.com' AS x)
SELECT  [CreatedDate] = X.value('/x:storage[1]/@created-on', 'NVARCHAR(MAX)'),
        [Country] = X.value('/x:storage[1]/x:country[1]', 'NVARCHAR(MAX)'),
        [State] = X.value('/x:storage[1]/x:state[1]', 'NVARCHAR(MAX)'),
        [PeriodType] = X.value('/x:storage[1]/x:period[1]/@type', 'NVARCHAR(MAX)'),
        [From] = X.value('/x:storage[1]/x:period[1]/@from', 'NVARCHAR(MAX)'),
        [To] = X.value('/x:storage[1]/x:period[1]/@to', 'NVARCHAR(MAX)'),
        [capacity-total mwh] = X.value('/x:storage[1]/x:capacity-total[1]/@mwh', 'NVARCHAR(MAX)'),
        [capacity-total mcm] = X.value('/x:storage[1]/x:capacity-total[1]/@mcm', 'NVARCHAR(MAX)'),
        [country-percentage] = X.value('/x:storage[1]/x:capacity-total[1]/@country-percentage', 'NVARCHAR(MAX)')
FROM    @T;

SQL小提琴

于 2013-01-29T13:39:17.673 回答