使用T-SQL

时间:2018-06-01 19:19:19

标签: sql-server xml xpath xquery

我有以下格式,我需要使用SSMS获取XML格式

<ESBTimecardLoad xmlns="http://test.com/schemas">
      <Initialize xmlns="http://test.com/schemas">
            <Add>
                  <TimeCard>
                        <Attributes>
                              <Timekeeper AliasField="Number">C1235</Timekeeper>
                              <WorkDate>5/11/2018</WorkDate>
                              <WorkHrs>1.50</WorkHrs>
                              <Matter AliasField="Number">04420.3004</Matter>
                              <WorkType>Admin</WorkType>
                              <Phase AliasField="Code">rty</Phase>
                              <Task AliasField="Code">QEW</Task>
                              <Activity AliasField="Code">TTT</Activity>                  
                        </Attributes>
                  </TimeCardPending>
            </Add>
            <Add>
                  <TimeCardPending>
                        <Attributes>
                              <Timekeeper AliasField="Number">C888888</Timekeeper>
                              <WorkDate>7/6/2017</WorkDate>
                              <WorkHrs>0.25</WorkHrs>
                              <Matter AliasField="Number">01594.1009</Matter>
                              <WorkType>Enterprise</WorkType>
                              <Phase AliasField="Code">P3</Phase>
                              <Task AliasField="Code">QWE</Task>
                              <Activity AliasField="Code">YYY</Activity>
                        </Attributes>
                  </TimeCardPending>
            </Add>
</Start>
</TimecardLoad>

我有以下查询来获取XML。请注意,每个记录都会重复添加,时间卡和属性。是否可以将每个记录的3个标签组合在一起?我无法为每条记录创建重复分组和Aliasfield部分。

WITH XMLNAMESPACES (DEFAULT 'http://Test.com/schemas') 
   SELECT
       CAST ((SELECT TOP 2 
                  [Team] "Timekeeper", 
                  [Date] "WorkDate",
                  [WorkHrs],
                  [Matter] "Matter",
                  [WorkType] "WorkType",
                  [Phase], [task],
                  [Activity], [Notes] AS Narrative    
              FROM
                  view_review     
              FOR XML PATH('Attributes'), ROOT('Initialize')) AS XML)
FOR XML PATH('TimeCardLoad'), ELEMENTS

这是我的结果:

<ESBTimeCardLoad xmlns="http://test.com/schemas">
  <Initialize xmlns="http://test.com/schemas">
    <Attributes>
      <Timekeeper>TEster, Monica</Timekeeper>
      <WorkDate>05/25/15</WorkDate>
      <WorkHrs>10.00</WorkHrs>
      <Phase>P3</Phase>
      <task>P3_Fsol</task>
      <Activity>SETUPS3</Activity>
    </Attributes>
    <Attributes>
      <Timekeeper>Mob, JOhn</Timekeeper>
      <WorkDate>05/27/15</WorkDate>
      <WorkHrs>8.00</WorkHrs>
      <Phase>P3</Phase>
      <task>P3_Fsol</task>
      <Activity>SETUPS3</Activity>
    </Attributes>
  </Initialize>
</ESBTimeCardLoad>

我应该以哪种方式更改查询?

1 个答案:

答案 0 :(得分:0)

您向我们展示的XML是我需要获取XML的格式在很多方面都不是很好的形式:

  • 您使用<ESBTimecardLoad><Initialize>打开,但以</Start></TimecardLoad>
  • 结尾
  • <Add>的第一个<TimeCard>内,您使用<TimeCardPending>打开,但以5/11/2018
  • 结尾
  • 您不应该在日期中使用与文化相关的格式。 DECLARE @tbl TABLE(ID INT IDENTITY, TimeKeeper VARCHAR(100),WorkDate DATE, WorkHrs DECIMAL(6,2), Matter VARCHAR(100)); INSERT INTO @tbl VALUES ('C1235','2018-11-05',1.5,04420.3004) ,('C8888','2017-06-07',0.25,01594.1009); WITH XMLNAMESPACES (DEFAULT 'http://Test.com/schemas') SELECT ( SELECT 'Number' AS [TimeCardPending/Attributes/Timekeeper/@AliasField] ,TimeKeeper AS [TimeCardPending/Attributes/Timekeeper] ,WorkHrs AS [TimeCardPending/Attributes/WorkHrs] ,'Number' AS [TimeCardPending/Attributes/Matter/@AliasField] ,Matter AS [TimeCardPending/Attributes/Matter] --and so on FROM @tbl t FOR XML PATH('Add'),ROOT('Initialize'),TYPE ) AS [*] FOR XML PATH(''),ROOT('ESBTimecardLoad'); 可能被视为 11月5日 5月11日 ......

以下内容将显示您的某些列的原则。看看吧。

<ESBTimecardLoad xmlns="http://Test.com/schemas">
  <Initialize xmlns="http://Test.com/schemas">
    <Add>
      <TimeCardPending>
        <Attributes>
          <Timekeeper AliasField="Number">C1235</Timekeeper>
          <WorkHrs>1.50</WorkHrs>
          <Matter AliasField="Number">4420.3004</Matter>
        </Attributes>
      </TimeCardPending>
    </Add>
    <Add>
      <TimeCardPending>
        <Attributes>
          <Timekeeper AliasField="Number">C8888</Timekeeper>
          <WorkHrs>0.25</WorkHrs>
          <Matter AliasField="Number">1594.1009</Matter>
        </Attributes>
      </TimeCardPending>
    </Add>
  </Initialize>
</ESBTimecardLoad>

结果

admin.py