基本上我需要从数据库输出单值和多值字段.数据的大多数用户都不是技术用户,如果他们能够阅读XML并理解它,那就太好了.在另一篇文章的帮助下,我几乎一直都在那里.
我遇到的问题是元素名称不能有任何空格.需要包含可能包含空格的显示名称.例如,MVtext displayName =“Multi Value Text”.如果正确的答案是内联架构或其他方法,请告诉我.它不像我可以发布静态模式,因为应用程序可以动态添加字段.字段可以随报告类型而变化.这些字段在表格中,我读取该表格来构建选择.
如何传达可能包含元素空间的显示名称? sql 2008 R2.
SELECT top 4 [sv].[sID] AS '@sID',[sv].[sParID] AS '@sParID',[sv].[docID] AS 'docID',[sv].addDate as 'addDate',(SELECT [value] AS 'value' FROM [docMVtext] as [mv] WHERE [mv].[sID] = [sv].[sID] AND [mv].[fieldID] = '113' ORDER BY [mv].[value] FOR XML PATH (''),type ) AS "To",(SELECT [value] AS 'value' FROM [docMVtext] as [mv] WHERE [mv].[sID] = [sv].[sID] AND [mv].[fieldID] = '130' ORDER BY [mv].[value] FOR XML PATH (''),type ) AS "MVtest" FROM [docSVsys] as [sv] WHERE [sv].[sID] >= '57' ORDER BY [sv].[sParID],[sv].[sID] FOR XML PATH('Document'),root('Documents')
生产:
<Documents> <Document sID="57" sParID="57"> <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA</docID> <addDate>2011-10-28T12:26:00</addDate> <To> <value>Frank Ermis</value> <value>Keith Holst</value> <value>Mike Grigsby</value> </To> <MVtest> <value>MV test 01</value> <value>MV test 02</value> <value>MV test 03</value> <value>MV test 04</value> </MVtest> </Document> <Document sID="58" sParID="57"> <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA.1</docID> <addDate>2011-10-28T12:26:00</addDate> </Document> <Document sID="59" sParID="59"> <docID>3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA</docID> <addDate>2011-10-28T12:26:00</addDate> <To> <value>Vladimir Gorny</value> </To> </Document> <Document sID="60" sParID="59"> <docID>3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA.1</docID> <addDate>2011-10-28T12:26:00</addDate> </Document> </Documents>
我累了
SELECT [value] AS 'value','MV test' as 'dispName'
但我明白了
<MVtest> <value>MV test 01</value> <dispName>MV test</dispName> <value>MV test 02</value> <dispName>MV test</dispName> <value>MV test 03</value> <dispName>MV test</dispName> <value>MV test 04</value> <dispName>MV test</dispName> </MVtest>
SELECT [value] AS’value’,’MV test’为’@dispName’
引发执行错误
行标记省略(空行标记名称)不能与以属性为中心的FOR XML序列化一起使用
期望的输出:
<Documents> <Document sID="57" sParID="57"> <docID>3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA</docID> <addDate>2011-10-28T12:26:00</addDate> <To> <value>Frank Ermis</value> <value>Keith Holst</value> <value>Mike Grigsby</value> </To> <MVtest dispName="Multi Value Text"> <value>MV test 01</value> <value>MV test 02</value> <value>MV test 03</value> <value>MV test 04</value> </MVtest> </Document> </Documents>
解:
SELECT top 4 [sv].[sID] AS '@sID','SV' as 'docID/@SVMV','SV' as 'addDate/@SVMV','Email To' as 'To/@DisplayName','MV' as 'To/@SVMV','Multi Value Text Sample' as 'MVtext130/@DisplayName','MV' as 'MVtext130/@SVMV',(SELECT [value] AS 'value' FROM [docMVtext] as [mv] WHERE [mv].[sID] = [sv].[sID] AND [mv].[fieldID] = '130' ORDER BY [mv].[value] FOR XML PATH (''),type ) AS "MVtext130" FROM [docSVsys] as [sv] WHERE [sv].[sID] >= '57' ORDER BY [sv].[sParID],root('Documents')
<Documents> <Document sID="57" sParID="57"> <docID SVMV="SV">3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA</docID> <addDate SVMV="SV">2011-10-28T12:26:00</addDate> <To DisplayName="Email To" SVMV="MV"> <value>Frank Ermis</value> <value>Keith Holst</value> <value>Mike Grigsby</value> </To> <MVtext130 DisplayName="Multi Value Text Sample" SVMV="MV"> <value>MV test 01</value> <value>MV test 02</value> <value>MV test 03</value> <value>MV test 04</value> </MVtext130> </Document> <Document sID="58" sParID="57"> <docID SVMV="SV">3.818919.C41P3UKK00BRICLAY0AR1ET2EBPYSU4SA.1</docID> <addDate SVMV="SV">2011-10-28T12:26:00</addDate> <To DisplayName="Email To" SVMV="MV" /> <MVtext130 DisplayName="Multi Value Text Sample" SVMV="MV" /> </Document> <Document sID="59" sParID="59"> <docID SVMV="SV">3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA</docID> <addDate SVMV="SV">2011-10-28T12:26:00</addDate> <To DisplayName="Email To" SVMV="MV"> <value>Vladimir Gorny</value> </To> <MVtext130 DisplayName="Multi Value Text Sample" SVMV="MV" /> </Document> <Document sID="60" sParID="59"> <docID SVMV="SV">3.818920.KJKP5LYKTNIODOEI4JDOKJ2BXJI5P0BIA.1</docID> <addDate SVMV="SV">2011-10-28T12:26:00</addDate> <To DisplayName="Email To" SVMV="MV" /> <MVtext130 DisplayName="Multi Value Text Sample" SVMV="MV" /> </Document> </Documents>
对于我提出的问题,这是一个很好的解决方案.添加属性的一个症状是即使零值行也能获得元素.理想情况下,如果没有值,它将不会列出元素.
尝试了一个Case语句,但即使我将值设置为”,它也会显示该元素.
,[MVtext130/@DisplayName] = Case (select COUNT(*) FROM [docMVtext] WHERE [docMVtext].[sID] = [sv].[sID] AND [docMVtext].[fieldID] = '130') when '0' then '' else 'Multi Value Text Sample' end
更新了没有值列表元素的解决方案:
SELECT top 4 [sv].[sID] AS '@sID',(select top(1) 'Email To' from [docMVtext] as C where C.[sID] = [sv].[sID] and c.fieldID = '113' ) as 'To/@DisplayName',(select top(1) 'MV' from [docMVtext] as C where C.[sID] = [sv].[sID] and c.fieldID = '113' ) as 'To/@SVMV' --,'Email To' as 'To/@DisplayName' --,(select top(1) 'Multi Value Text Sample' from [docMVtext] as C where C.[sID] = [sv].[sID] and c.fieldID = '130' ) as 'MVtext130/@DisplayName',(select top(1) 'MV' from [docMVtext] as C where C.[sID] = [sv].[sID] and c.fieldID = '130' ) as 'MVtext130/@SVMV' --,'Multi Value Text Sample' as 'MVtext130/@DisplayName' --,root('Documents')
不确定我理解你想要什么,如果没有你的表,很难用你的代码做一些事情,所以我创建了一个我认为正在做你已经拥有的样本.
原文链接:https://www.f2er.com/xml/293024.html设置表和数据:
declare @Main table ( MainID int identity,Value int ) declare @Child table ( ChildID int identity,MainID int,Value int ) insert into @Main values (10),(20),(30) insert into @Child values (1,100),(2,200),210)
select M.MainID as '@MainID',M.Value as 'MainValue',(select C.Value as ChildValue from @Child as C where C.MainID = M.MainID for xml path(''),type) as Child from @Main as M for xml path('Document'),root('Documents')
结果:
<Documents> <Document MainID="1"> <MainValue>10</MainValue> <Child> <ChildValue>100</ChildValue> </Child> </Document> <Document MainID="2"> <MainValue>20</MainValue> <Child> <ChildValue>200</ChildValue> <ChildValue>210</ChildValue> </Child> </Document> <Document MainID="3"> <MainValue>30</MainValue> </Document> </Documents>
<Documents> <Document MainID="1"> <MainValue>10</MainValue> <Child DisplayName="Child Display Name"> <ChildValue>100</ChildValue> </Child> </Document> <Document MainID="2"> <MainValue>20</MainValue> <Child DisplayName="Child Display Name"> <ChildValue>200</ChildValue> <ChildValue>210</ChildValue> </Child> </Document> <Document MainID="3"> <MainValue>30</MainValue> </Document> </Documents>
为此,您可以使用此查询:
select M.MainID as '@MainID',(select top(1) 'Child Display Name' from @Child as C where C.MainID = M.MainID) as 'Child/@DisplayName',root('Documents')