将每个子标记转换为具有多个分隔符的单个列-SQL Server(3)

时间:2017-06-13 07:25:02

标签: sql-server xml tsql xpath xquery

我的xml:

declare @x xml='<Detail>
    <ROll ID="1">
         <Exams>
                <Examdetails date="2017-04-02 13:30:00">
                  <Exam name="ECO" Total="100">150</Exam>
                  <Exam name="BIO" Total="150">50</Exam>
                  <Exam name="MATH" Total="200">28</Exam>
                </Examdetails>

                <Examdetails date="2017-04-02 14:30:00">
                  <Exam name="ENGLISH" Total="100">150</Exam>
                  <Exam name="BIO" Total="200">50</Exam>
                  <Exam name="ZIO" Total="250">28</Exam>
                </Examdetails>
         </Exams>
    </ROll> 
    <ROll ID="2">
         <Exams>
                <Examdetails date="2017-05-02 13:30:00">
                  <Exam name="HIS" Total="100">150</Exam>
                  <Exam name="BIO" Total="200">50</Exam>
                  <Exam name="THI" Total="200">89</Exam>
                </Examdetails>
         </Exams>
    </ROll>
</Detail>'

我想基于ROLL ID隔离我的xml,而我尝试使用here引用的以下查询

SELECT STUFF(
(
    SELECT '!' + STUFF(p.query(N'for $n in .//*
                           return <a>{concat("$",($n/text())[1])}</a>'
                        ).value(N'.',N'nvarchar(max)'),1,1,'')
    FROM p.nodes(N'Exams') AS A(p)
    FOR XML PATH(''),TYPE).value(N'.',N'nvarchar(max)'),1,1,'')
FROM @x.nodes(N'Detail/ROll') AS A(p);

我得到结果

enter image description here

但我想以“

”的形式查询它
2017-04-02 13:30:00$ECO$100$150!2017-04-02 13:30:00$BIO$150$50!2017-04-02 13:30:00$MATH$200$28!2017-04-02 14:30:00$ENGLISH$100$150!2017-04-02 14:30:00$BIO$200$50!2017-04-02 14:30:00$ZIO$250$28
2017-05-02 13:30:00$HIS$100$150!2017-05-02 13:30:00$BIO$200$50!2017-05-02 13:30:00$THI$200$89

请帮助我解决这种复杂性

提前致谢,Jayendran

1 个答案:

答案 0 :(得分:1)

在这种情况下,我会离开通用路径并按照以下方式构建它:

SELECT r.value(N'@ID',N'int') AS ROll_ID
,STUFF((
    SELECT 
           (
           SELECT '!'+ed.value(N'@date',N'nvarchar(max)')
                 +'$' + e.value(N'@name','nvarchar(max)')
                 +'$' + e.value(N'@Total','nvarchar(max)')
                 +'$' + e.value(N'text()[1]','nvarchar(max)')
           FROM ed.nodes(N'Exam') AS D(e)
           FOR XML PATH(''),TYPE
           ).value(N'text()[1]','nvarchar(max)')  
    FROM ex.nodes(N'Examdetails') AS C(ed)
    FOR XML PATH(''),TYPE
).value(N'text()[1]','nvarchar(max)'),1,1,'')
FROM @x.nodes(N'/Detail/ROll') AS A(r)
CROSS APPLY r.nodes(N'Exams') AS B(ex);

通用解决方案可能不起作用的主要问题:

  • 很难混合属性和元素text()一般
  • 重要!)属性顺序无法保证!属性值可能不按预期顺序显示...