从SQL Server的XML数据类型字段中提取数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从SQL Server的XML数据类型字段中提取数据相关的知识,希望对你有一定的参考价值。
我在ReportServer数据库上执行以下查询。
SELECT ItemID, CAST(CAST(Content AS VARBINARY(MAX)) AS XML) Content
FROM dbo.Catalog
WHERE Type = 8
我想提取Content
列的字段列表。 content
列是XML
数据类型,并包含XML,如下所示:
<SharedDataSet xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/shareddatasetdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSet Name="DataSet1">
<Query>
<DataSourceReference>DDSDB</DataSourceReference>
<CommandText>SELECT z.AccessoryPercent
,CASE
WHEN z.AccessoryPercent>0.20 Then 1
WHEN z.AccessoryPercent >=0.10 THEN 0
ELSE -1
end AS AccessoryState
FROM (
SELECT (a.FixedPrice - a.NetSalesPrice)/a.FixedPrice AS AccessoryPercent
FROM acc.Fact_Crm_Accessory a
)z</CommandText>
</Query>
<Fields>
<Field Name="AccessoryPercent">
<DataField>AccessoryPercent</DataField>
<rd:TypeName>System.Decimal</rd:TypeName>
</Field>
<Field Name="AccessoryState">
<DataField>AccessoryState</DataField>
<rd:TypeName>System.Int32</rd:TypeName>
</Field>
</Fields>
</DataSet>
</SharedDataSet>
我的预期结果是:
DataSetName Field
-------------------------------------
DataSet1 AccessoryPercent
DataSet1 AccesspryState
提前感谢
EDIT:我编写了以下查询,以简化查询工作。
CREATE TABLE #t (data XML)
INSERT INTO #t
VALUES('<SharedDataSet xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/shareddatasetdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSet Name="DataSet1">
<Query>
<DataSourceReference>DDSDB</DataSourceReference>
<CommandText>SELECT z.AccessoryPercent
,CASE
WHEN z.AccessoryPercent>0.20 Then 1
WHEN z.AccessoryPercent >=0.10 THEN 0
ELSE -1
end AS AccessoryState
FROM (
SELECT (a.FixedPrice - a.NetSalesPrice)/a.FixedPrice AS AccessoryPercent
FROM acc.Fact_Crm_Accessory a
)z</CommandText>
</Query>
<Fields>
<Field Name="AccessoryPercent">
<DataField>AccessoryPercent</DataField>
<TypeName>System.Decimal</TypeName>
</Field>
<Field Name="AccessoryState">
<DataField>AccessoryState</DataField>
<TypeName>System.Int32</TypeName>
</Field>
</Fields>
</DataSet>
</SharedDataSet>')
SELECT *
FROM #t AS t
答案
((1)我们需要考虑默认名称空间。
((2)Second CROSS APPLY
处理数据集及其字段之间的一对多关系的情况。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (xmldata XML);
INSERT INTO @tbl
VALUES('<SharedDataSet xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/shareddatasetdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<DataSet Name="DataSet1">
<Query>
<DataSourceReference>DDSDB</DataSourceReference>
<CommandText>SELECT z.AccessoryPercent
,CASE
WHEN z.AccessoryPercent>0.20 Then 1
WHEN z.AccessoryPercent >=0.10 THEN 0
ELSE -1
end AS AccessoryState
FROM (
SELECT (a.FixedPrice - a.NetSalesPrice)/a.FixedPrice AS AccessoryPercent
FROM acc.Fact_Crm_Accessory a
)z</CommandText>
</Query>
<Fields>
<Field Name="AccessoryPercent">
<DataField>AccessoryPercent</DataField>
<TypeName>System.Decimal</TypeName>
</Field>
<Field Name="AccessoryState">
<DataField>AccessoryState</DataField>
<TypeName>System.Int32</TypeName>
</Field>
</Fields>
</DataSet>
</SharedDataSet>');
-- DDL and sample data population, end
;WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/reporting/2010/01/shareddatasetdefinition')
SELECT c.value('@Name','VARCHAR(30)') AS DataSetName
, f.value('@Name','VARCHAR(30)') AS Field
FROM @tbl AS tbl
CROSS APPLY tbl.xmldata.nodes('/SharedDataSet/DataSet') AS t(c)
CROSS APPLY t.c.nodes('Fields/Field') AS f(f);
输出
+-------------+------------------+
| DataSetName | Field |
+-------------+------------------+
| DataSet1 | AccessoryPercent |
| DataSet1 | AccessoryState |
+-------------+------------------+
以上是关于从SQL Server的XML数据类型字段中提取数据的主要内容,如果未能解决你的问题,请参考以下文章
如何在 SQL Server 中使用 XML 节点传递 NULL 而不是字符串格式的“NULL”值
如何从以 BLOB 类型存储在列中的 XML 中提取数据(通过 SQL 查询)
从包含十进制 SQL Server 18 的字符串字段中提取数字