2015-02-09 48 views
0

我有一個表T1與以下字段:ID,姓氏,名稱,BirthDate,BirthPlace,DocNumber,IssueDate。 我已經使用MS SQL查詢創建嵌套的XML

select * from T1 
for XML PATH ('Person') 

創建的XML這造就了我用XML格式如下

<Person> 
    <ID>1</ID> 
    <SurName>Ivanov</SurName> 
    ... 
</Person> 
<Person> 
... 
</Person> 
... 

此XML僅包含一個元素,這是「人」,我想創造另一個元素(「文件')在此元素內,類似於:

<Person> 
    <ID>1</ID> 
    <SurName>Ivanov</SurName> 
    ... 
    <Documents> 
     <DocNumber>1234</DocNumber> 
     <IssueDate>'20140101'</IssueDate> 
    </Documents> 
</Person> 
... 

我該怎麼做?有什麼建議麼?提前致謝。

的樣本數據

ID SurName Name BirthDate BirthPlace DocNumber IssueDate 
1 Ivanov Ivan '19900212' Moscow  1111  '20121225' 
1 Ivanov Ivan '19900212' Moscow  2222  '20110629' 
2 Smirnov Petr '19851006' Kazan  3333  '20090601' 
2 Smirnov Petr '19851006' Kazan  4444  '20130930' 
+0

可以共享'schema'和T1'表 – 2015-02-09 11:16:00

+0

感謝大家的''樣品data'。這是有幫助的http://stackoverflow.com/questions/11658942/sql-server-for-xml-path-nesting-grouping – 2015-02-09 11:35:16

回答

0

你需要看一下FOR XML PATH選項,SQL Server 2005中引入了 - 見What's New in FOR XML in Microsoft SQL Server 2005文件以獲取更多信息。

基本上,使用FOR XML PATH,您可以非常輕鬆地定義XML的形狀。您可以定義某些結構,您可以定義某些列作爲屬性輸出,其他列作爲元素 - 完全在您的控制之下。

不知道你的表結構,我只能猜測一下表和列被稱爲你的情況 - 但你也許可以寫類似:

Select 
      tt1.ID AS 'ID', 
      tt1.Surname As 'Surname', 
      ... 
      ( 
      Select 
       tt2.DocNumber As 'DocNumber', 
       tt2.Issudate AS 'IssueDate', 
      From 
       T1 as tt2 WITH(NOLOCK) 
      Where 
       tt2.ID = tt1.ID 
      for xml path('Documents'), type 
      ) 
    From 
     t1 as tt1 
    For xml path ('Person') 
0

你需要一個子查詢,以提供所需的嵌套:

SQL Fiddle

MS SQL Server 2012的架構設置

CREATE TABLE T1 
     ([ID] int, [SurName] varchar(7), [Name] varchar(4), 
     [BirthDate] datetime, [BirthPlace] varchar(6), 
     [DocNumber] int, [IssueDate] datetime) 
    ; 

INSERT INTO T1 
    ([ID], [SurName], [Name], [BirthDate], [BirthPlace], [DocNumber], [IssueDate]) 
VALUES 
    (1, 'Ivanov', 'Ivan', '1990-02-12 00:00:00', 'Moscow', 1111, '2012-12-25 00:00:00'), 
    (1, 'Ivanov', 'Ivan', '1990-02-12 00:00:00', 'Moscow', 2222, '2011-06-29 00:00:00'), 
    (2, 'Smirnov', 'Petr', '1985-10-06 00:00:00', 'Kazan', 3333, '2009-06-01 00:00:00'), 
    (2, 'Smirnov', 'Petr', '1985-10-06 00:00:00', 'Kazan', 4444, '2013-09-30 00:00:00') 
; 

查詢1

SELECT [ID], [SurName], [Name], [BirthDate], [BirthPlace], 
(SELECT [DocNumber], [IssueDate] 
FROM T1 AS T2 
WHERE T1.ID = T2.ID 
FOR XML PATH ('Document'), type) 
FROM T1 
GROUP BY [ID], [SurName], [Name], [BirthDate], [BirthPlace] 
FOR XML PATH ('Person') 

產地:

<Person> 
    <ID>1</ID> 
    <SurName>Ivanov</SurName> 
    <Name>Ivan</Name> 
    <BirthDate>1990-02-12T00:00:00</BirthDate> 
    <BirthPlace>Moscow</BirthPlace> 
    <Document> 
    <DocNumber>1111</DocNumber> 
    <IssueDate>2012-12-25T00:00:00</IssueDate> 
    </Document> 
    <Document> 
    <DocNumber>2222</DocNumber> 
    <IssueDate>2011-06-29T00:00:00</IssueDate> 
    </Document> 
</Person> 
<Person> 
    <ID>2</ID> 
    <SurName>Smirnov</SurName> 
    <Name>Petr</Name> 
    <BirthDate>1985-10-06T00:00:00</BirthDate> 
    <BirthPlace>Kazan</BirthPlace> 
    <Document> 
    <DocNumber>3333</DocNumber> 
    <IssueDate>2009-06-01T00:00:00</IssueDate> 
    </Document> 
    <Document> 
    <DocNumber>4444</DocNumber> 
    <IssueDate>2013-09-30T00:00:00</IssueDate> 
    </Document> 
</Person>