Sql 从具有XML数据类型的表中查询XML数据
我有一个id为、createdate为、outputxml为列的表,其中outputxml是一个XML数据类型列,其信息如下Sql 从具有XML数据类型的表中查询XML数据,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个id为、createdate为、outputxml为列的表,其中outputxml是一个XML数据类型列,其信息如下 <storage xmlns="http://google.com " created-on="2012-12-29"> <country>India</country> <state>tn</point> <period type="
<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>
但它不会返回任何值。因此,请建议一种解决方案。您需要定义并在访问值时使用:
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;
DECLARE@T表(xxml);
插入@T值('
印度
总氮
');
使用XMLNAMESPACES('http://google.com'作为x)
选择[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)'),
[总容量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)'),
[国家/地区百分比]=X.value('/X:storage[1]/X:capacity total[1]/@country percentage','NVARCHAR(MAX)')
来自@T;
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;