2011-04-14 162 views
1

我有一個MS SQL 2005數據庫,其中報告表有XMLReport柱與XML的結構與此類似:SQL查詢來獲取所有實例中嵌套的XML

<my:CART_Marine> 
    <my:Area_Summary_Details> 
     <my:Areas> 
      <my:Area_Group> 
       <my:Area_Number>1</my:Area_Number> 
       <my:Paint_Application> 
        <my:Paint_Applications> 
         <my:Paint_Application_Group> 
          <my:Coat_Number>1</my:Coat_Number> 
          <my:Base_Batches> 
           <my:Base_Batch_Group> 
            <my:Base_Batch_No>1234567</my:Base_Batch_No> 
            <my:Base_Batch_No>5455443</my:Base_Batch_No> 
            <my:Base_Batch_No>8677667</my:Base_Batch_No> 
            <my:Base_Batch_No>3455445</my:Base_Batch_No> 
           </my:Base_Batch_Group> 
          </my:Base_Batches> 
         </my:Paint_Application_Group> 
         <my:Paint_Application_Group> 
          <my:Coat_Number>2</my:Coat_Number> 
          <my:Base_Batches> 
           <my:Base_Batch_Group> 
            <my:Base_Batch_No>9744566</my:Base_Batch_No> 
            <my:Base_Batch_No>8755632</my:Base_Batch_No> 
           </my:Base_Batch_Group> 
          </my:Base_Batches> 
         </my:Paint_Application_Group> 
         <my:Paint_Application_Group> 
          <my:Coat_Number>3</my:Coat_Number> 
          <my:Base_Batches> 
           <my:Base_Batch_Group> 
            <my:Base_Batch_No>5456783</my:Base_Batch_No> 
           </my:Base_Batch_Group> 
          </my:Base_Batches> 
         </my:Paint_Application_Group> 
        </my:Paint_Applications> 
       </my:Paint_Application> 
      </my:Area_Group> 
      <my:Area_Group> 
       <my:Area_Number>2</my:Area_Number> 
       <my:Paint_Application> 
        <my:Paint_Applications> 
         <my:Paint_Application_Group> 
          <my:Coat_Number>1</my:Coat_Number> 
          <my:Base_Batches> 
           <my:Base_Batch_Group> 
            <my:Base_Batch_No>2312311</my:Base_Batch_No> 
            <my:Base_Batch_No>2352244</my:Base_Batch_No> 
            <my:Base_Batch_No>8746773</my:Base_Batch_No> 
            <my:Base_Batch_No>7363634</my:Base_Batch_No> 
           </my:Base_Batch_Group> 
          </my:Base_Batches> 
         </my:Paint_Application_Group> 
        </my:Paint_Applications> 
       </my:Paint_Application> 
      </my:Area_Group> 
      <my:Area_Group> 
       <my:Area_Number>3</my:Area_Number> 
       <my:Paint_Application> 
        <my:Paint_Applications> 
         <my:Paint_Application_Group> 
          <my:Coat_Number>1</my:Coat_Number> 
          <my:Base_Batches> 
           <my:Base_Batch_Group> 
            <my:Base_Batch_No>1523552</my:Base_Batch_No> 
            <my:Base_Batch_No>6164633</my:Base_Batch_No> 
           </my:Base_Batch_Group> 
          </my:Base_Batches> 
         </my:Paint_Application_Group> 
        </my:Paint_Applications> 
       </my:Paint_Application> 
      </my:Area_Group> 
     </my:Areas>      
    </my:Area_Summary_Details>       
</my:CART_Marine>        

AREA_GROUPPaint_Application_GroupBase_Batch_Group是可重複的

我想要實現的是一張帶有圓柱ns:

區域編號 | Coat_Number | Base_Batch_No

其中將Coat_Number僅從指定Area_NumberBase_Batch_No將只能從指定Coat_Number

基於上面的例子中,應該創造這樣的事情:

**Area_Number** |**Coat_Number** |**Base_Batch_Number** 

1  |1  |1234567 

1  |1  |5455443 

1  |1  |8677667 

1  |1  |3455445 

1  |2  |9744566 

1  |2  |8755632 

1  |3  |5456783 

2  |1  |2312311 

2  |1  |2352244 

2  |1  |8746773 

2  |1  |7363634 

3  |1  |1523552 

3  |1  |6164633 

我嘗試過很多辦法,我的是這樣完成:

select distinct 
     r.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Area_Number)[1]','nvarchar(12)') AS Area_Number, 
     s.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Paint_Application/my:Paint_Applications/my:Paint_Application_Group/my:Coat_Number)[1]','nvarchar(12)') AS Coat_Number, 
     t.XmlReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Paint_Application/my:Paint_Applications/my:Paint_Application_Group/my:Base_Batches/my:Base_Batch_Group/my:Base_Batch_No)[1]','nvarchar(12)') AS Base_Batch_Number 
from Report 
     cross apply Report.XMLReport.nodes('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(/my:CART_Marine/my:Area_Summary_Details/my:Areas/my:Area_Group)') AS s(XMLReport) 
     cross apply Report.XMLReport.nodes('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(/my:CART_Marine/my:Area_Summary_Details/my:Areas/my:Area_Group)') AS r(XMLReport) 
     cross apply Report.XMLReport.nodes('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(/my:CART_Marine/my:Area_Summary_Details/my:Areas/my:Area_Group)') AS t(XMLReport) 

where 
     r.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Area_Number)[1]','nvarchar(12)')= s.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Area_Number)[1]','nvarchar(12)') 
    AND 
     s.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Paint_Application/my:Paint_Applications/my:Paint_Application_Group/my:Coat_Number)[1]','nvarchar(12)')= t.XMLReport.value('declare namespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-02T08:49:16";(my:Paint_Application/my:Paint_Applications/my:Paint_Application_Group/my:Coat_Number)[1]','nvarchar(12)') 

這是如下製作表格(我從頭腦裏創建了這張桌子,所以它不能顯示所有的行,但你會發現這個想法)

**Area_Number** |**Coat_Number** |**Base_Batch_Number** 

1   |1    |1234567 

1   |1    |5455443 

1   |1    |8677667 

1   |1    |3455445 

2   |1    |2312311 

3   |1    |1523552 

所以只有第一個Coat_Number是包括在內,只有第一個Base_Batch_Number在第一個Coat_Number包括在內。

我無法弄清楚如何查詢可以通過Paint_Application_GroupBase_Batch_Group組的所有實例進行迭代。

請幫幫忙,我fightenig了現在是3天... ...

剋日什托夫·Deneka

回答

0

我刪除了命名空間爲簡單起見,但這裏的想法:

DECLARE @report XML = N' 
<CART_Marine> 
    <Area_Summary_Details> 
     <Areas> 
      <Area_Group> 
       <Area_Number>1</Area_Number> 
       <Paint_Application> 
        <Paint_Applications> 
         <Paint_Application_Group> 
          <Coat_Number>1</Coat_Number> 
          <Base_Batches> 
           <Base_Batch_Group> 
            <Base_Batch_No>1234567</Base_Batch_No> 
            <Base_Batch_No>5455443</Base_Batch_No> 
            <Base_Batch_No>8677667</Base_Batch_No> 
            <Base_Batch_No>3455445</Base_Batch_No> 
           </Base_Batch_Group> 
          </Base_Batches> 
         </Paint_Application_Group> 
         <Paint_Application_Group> 
          <Coat_Number>2</Coat_Number> 
          <Base_Batches> 
           <Base_Batch_Group> 
            <Base_Batch_No>9744566</Base_Batch_No> 
            <Base_Batch_No>8755632</Base_Batch_No> 
           </Base_Batch_Group> 
          </Base_Batches> 
         </Paint_Application_Group> 
         <Paint_Application_Group> 
          <Coat_Number>3</Coat_Number> 
          <Base_Batches> 
           <Base_Batch_Group> 
            <Base_Batch_No>5456783</Base_Batch_No> 
           </Base_Batch_Group> 
          </Base_Batches> 
         </Paint_Application_Group> 
        </Paint_Applications> 
       </Paint_Application> 
      </Area_Group> 
      <Area_Group> 
       <Area_Number>2</Area_Number> 
       <Paint_Application> 
        <Paint_Applications> 
         <Paint_Application_Group> 
          <Coat_Number>1</Coat_Number> 
          <Base_Batches> 
           <Base_Batch_Group> 
            <Base_Batch_No>2312311</Base_Batch_No> 
            <Base_Batch_No>2352244</Base_Batch_No> 
            <Base_Batch_No>8746773</Base_Batch_No> 
            <Base_Batch_No>7363634</Base_Batch_No> 
           </Base_Batch_Group> 
          </Base_Batches> 
         </Paint_Application_Group> 
        </Paint_Applications> 
       </Paint_Application> 
      </Area_Group> 
      <Area_Group> 
       <Area_Number>3</Area_Number> 
       <Paint_Application> 
        <Paint_Applications> 
         <Paint_Application_Group> 
          <Coat_Number>1</Coat_Number> 
          <Base_Batches> 
           <Base_Batch_Group> 
            <Base_Batch_No>1523552</Base_Batch_No> 
            <Base_Batch_No>6164633</Base_Batch_No> 
           </Base_Batch_Group> 
          </Base_Batches> 
         </Paint_Application_Group> 
        </Paint_Applications> 
       </Paint_Application> 
      </Area_Group> 
     </Areas>      
    </Area_Summary_Details>       
</CART_Marine>' 

SELECT batch.value('../../../../../../Area_Number[1]', 'INTEGER'), 
     batch.value('../../../Coat_Number[1]', 'INTEGER'), 
     batch.value('.', 'INTEGER') 
FROM @report.nodes('//Base_Batch_No') r(batch) 
+0

這是充當魅力。非常感謝! – 2011-04-14 13:02:16