多个XML属性
我想,制成具有以下格式的XML文件:多个XML属性
<ProData>
<DataSet Name="ABCD">
<Data DataElement="AAA" Value="10"/>
<Data DataElement="BBB" Value="20"/>
</DataSet>
<DataSet Name="EFGH">
<Data DataElement="CCC" Value="NAME"/>
<Data DataElement="DDD" Value="SURNAME"/>
</DataSet>
</ProData>
从MyTable的每一行都应该有生成的XML一个ProData记录。 我的表是这样的
CREATE TABLE MyTable(
[CustomerNumber] [nvarchar](6) NOT NULL,
[AAA_Value] Int NOT NULL,
[BBB_Value] Int NOT NULL,
[Name] [nvarchar](10) NOT NULL,
[Surname] [nvarchar](10) NOT NULL
)
INSERT [dbo].[MyTable] ([CustomerNumber], [AAA_Value], [BBB_Value], [Name], [Surname]) VALUES (N'123456', 10, 20, N'Phoebe', N'Buffay')
INSERT [dbo].[MyTable] ([CustomerNumber], [AAA_Value], [BBB_Value], [Name], [Surname]) VALUES (N'234567', 30, 40, N'Ross', N'Geller')
因此所需的输出应该是:
<ProData>
<DataSet Name="ABCD">
<Data DataElement="AAA" Value="10"/>
<Data DataElement="BBB" Value="20"/>
</DataSet>
<DataSet Name="EFGH">
<Data DataElement="CCC" Value="Phoebe"/>
<Data DataElement="DDD" Value="Buffay"/>
</DataSet>
</ProData>
<ProData>
<DataSet Name="ABCD">
<Data DataElement="AAA" Value="30"/>
<Data DataElement="BBB" Value="40"/>
</DataSet>
<DataSet Name="EFGH">
<Data DataElement="CCC" Value="Ross"/>
<Data DataElement="DDD" Value="Geller"/>
</DataSet>
</ProData>
我使用的是FOR XML PATH
查询检索我的XML
文件,但我不能检索多个嵌套元素成功,因以下错误:
The same attribute cannot be generated more than once on the same XML tag.
我的查询如下:
(SELECT
blah,
'AAA' as 'Common/Data/ProData/DataSet/Data/@DataElement',
AAA_Value AS 'Common/ApplicationData/ProData/DataSet/Data/@Value' ,
'BBB' as 'Common/Data/ProData/DataSet/Data/@DataElement' ,
BBB_Value as 'Common/Data/ProData/DataSet/Data/@Value',
blah
FROM MyTable
FOR XML PATH('Notification'),ROOT('NotificationsList'),
TYPE)
FOR XML PATH ('NotificationFile')
我已成功通过“强制”中的结果与这样的嵌套查询,以获得期望的结果:
(SELECT blah,
(SELECT
(SELECT
(SELECT 'AAA' AS 'Data/@DataElement' ,
AAA_Value AS 'Data/@Value'
FROM MyTable WHERE CONDITION
FOR xml path(''), TYPE),
(SELECT 'BBB' AS 'Data/@DataElement' ,
BBB_Value AS 'Data/@Value'
FROM MyTable WHERE CONDITION
FOR xml path(''), TYPE),
FOR xml path('DataSet'), TYPE)
FOR xml path('ProData'), TYPE) ,
blah
FROM MyTable
FOR XML PATH('Notification'),ROOT('NotificationsList'),
TYPE )
FOR XML PATH ('NotificationFile')
我敢肯定,这是做一个可怕的方式,但我可以似乎没有为它管理更好的方式。 有人可以帮我吗?
谢谢
你澄清的你的需求,我认为这是这一点,你想要什么
- 您的表
CREATE TABLE MyTable(
[CustomerNumber] [nvarchar](6) NOT NULL,
[AAA_Value] Int NOT NULL,
[BBB_Value] Int NOT NULL,
[Name] [nvarchar](10) NOT NULL,
[Surname] [nvarchar](10) NOT NULL
)
- 测试数据
INSERT [dbo].[MyTable] ([CustomerNumber], [AAA_Value], [BBB_Value], [Name], [Surname])
VALUES (N'123456', 10, 20, N'Phoebe', N'Buffay')
INSERT [dbo].[MyTable] ([CustomerNumber], [AAA_Value], [BBB_Value], [Name], [Surname])
VALUES (N'234567', 30, 40, N'Ross', N'Geller');
GO
- 查询
SELECT
(
SELECT 'ABCD' AS [@Name]
,'AAA' AS [Data/@DataElement]
,AAA_Value AS [Data/@Value]
,''
,'BBB' AS [Data/@DataElement]
,BBB_Value AS [Data/@Value]
FOR XML PATH('DataSet'),TYPE
)
,''
,(
SELECT 'EFGH' AS [@Name]
,'CCC' AS [Data/@DataElement]
,Name AS [Data/@Value]
,''
,'DDD' AS [Data/@DataElement]
,Surname AS [Data/@Value]
FOR XML PATH('DataSet'),TYPE
)
FROM MyTable
FOR XML PATH('ProData')
GO
- 清理关注真实数据!
--DROP TABLE MyTable;
结果
<ProData>
<DataSet Name="ABCD">
<Data DataElement="AAA" Value="10" />
<Data DataElement="BBB" Value="20" />
</DataSet>
<DataSet Name="EFGH">
<Data DataElement="CCC" Value="Phoebe" />
<Data DataElement="DDD" Value="Buffay" />
</DataSet>
</ProData>
<ProData>
<DataSet Name="ABCD">
<Data DataElement="AAA" Value="30" />
<Data DataElement="BBB" Value="40" />
</DataSet>
<DataSet Name="EFGH">
<Data DataElement="CCC" Value="Ross" />
<Data DataElement="DDD" Value="Geller" />
</DataSet>
</ProData>
一个注意:空列之间(,''
)告诉引擎启动了新的元素。这将避免你的错误...
我应该清除“ABCD”,“AAA”,“BBB”,“EFGH”,“CCC”,“DDD”都是硬编码值,只有值来自表格。我会尽力调整你的答案,因为它看起来比我的简单得多,看看我能做些什么。谢谢! – NotApplicable
@NotApplicable,现在我明白了......看我的更新 – Shnugo
非常感谢Shnugo!那正是我想要的。 – NotApplicable
如何为我们提供您使用的示例数据,以便我们可以尝试协助? – Matt
我提供了表格的布局。谢谢! – NotApplicable
嗨,不,你没有提供你的表格布局,只是一些列名...请提供一个真正的表格定义(带类型)和一些样品数据行以及适合这个数据的预期输出... – Shnugo