Sql server xml根目录在sql中重复多次?
我希望xml的输出如下所示:Sql server xml根目录在sql中重复多次?,sql-server,xml,tsql,Sql Server,Xml,Tsql,我希望xml的输出如下所示: <CustomConfigurationSteps> <CustomConfigurations> <CustomConfiguration SiteURL="abc"> <CustomElements> <CustomElement Type="Searchtype" unit="3" price="5"> <ClassValue>A
<CustomConfigurationSteps>
<CustomConfigurations>
<CustomConfiguration SiteURL="abc">
<CustomElements>
<CustomElement Type="Searchtype" unit="3" price="5">
<ClassValue>AZSupreme</ClassValue>
</CustomElement>
</CustomElements>
<CustomElements>
<CustomElement Type="Searchtype" unit="3" price="5">
<ClassVaue>AZSupreme</ClassVaue>
</CustomElement>
</CustomElements>
</CustomConfiguration>
</CustomConfigurations>
</CustomConfigurationSteps>
像这样尝试(尽管这看起来有点奇怪,尤其是重复的
节点)
模拟问题的实体模型表:
DECLARE @tbl TABLE([Type] VARCHAR(100),unit int, price DECIMAL(10,4),ClassValue VARCHAR(100));
INSERT INTO @tbl VALUES('Searchtype 1',1,11,'AZSupreme 1')
,('Searchtype 2',2,22,'AZSupreme 2');
--询问
SELECT 'abc' AS [CustomConfiguration/@SiteURL]
,(
SELECT t.[Type] AS [CustomElement/@Type]
,t.unit AS [CustomElement/@unit]
,t.price AS [CustomElement/@pricee]
,t.ClassValue [CustomElement/ClassValue]
FROM @tbl t
FOR XML PATH('CustomElements'),TYPE
) AS CustomConfiguration
FOR XML PATH('CustomConfigurations'),ROOT('CustomConfigurationSteps');
XML支持是高度特定于供应商的-因此请添加一个标记,以指定您使用的是
mysql
,postgresql
,sql server
,oracle
或db2
,还是其他完全不同的东西。我使用的是sql server我尝试了这一点,但这仍然是自定义配置repeating@user3675493对不起,你没有在我的代码中显示的
(您声称是重复的)不够多是内部Xml Select的列名。只要不涉及其他步骤,如果多次调用此查询,则不应重复此操作…请尝试使用我的代码设置。一些独立的代码示例,我们可以复制到我们的计算机并运行以重现您的问题。
SELECT 'abc' AS [CustomConfiguration/@SiteURL]
,(
SELECT t.[Type] AS [CustomElement/@Type]
,t.unit AS [CustomElement/@unit]
,t.price AS [CustomElement/@pricee]
,t.ClassValue [CustomElement/ClassValue]
FROM @tbl t
FOR XML PATH('CustomElements'),TYPE
) AS CustomConfiguration
FOR XML PATH('CustomConfigurations'),ROOT('CustomConfigurationSteps');