2012-03-22 2 views
5

J'essaie de créer le document XML ci-dessous à partir d'une requête sql 2005. Ce que je struling avec est le doc a 3 nœuds sur le même niveau - Customer, RepairFacility, et Action.Créer un document XML à partir d'une requête SQL

--Qu'est est nécessaire

<Elements> 
    <Element> 
     <Customer> 
      <FirstName></FirstName> 
      <LastName></LastName> 
     </Customer> 
     <RepairFacility> 
      <LocationName></LocationName> 
      <LocationPhone></LocationPhone> 
     </RepairFacility> 
     <Action> 
      <FollowUpFlag></FollowUpFlag> 
      <DateAction></DateAction> 
     </Action> 
    </Element> 
    <Element> 
    </Element> 
</Elements> 

--Mon Recherche

SELECT 
    ( SELECT ..... 
FROM tbl A1 
FOR XML PATH('Customer'), TYPE), 
    (SELECT ...... 
FROM tbl  A2 
FOR XML PATH('RepairFacility'), TYPE), 
    (SELECT .....        
FROM tbl J 
FOR XML PATH('Action'),  TYPE ) 
FOR XML PATH(''), ROOT('Element') 

--Qu'est Je reçois

<Elements> 
    <Element> 
     <Customer> 
      <FirstName></FirstName> 
      <LastName></LastName> 
     </Customer> 
     <Customer> 
      <FirstName></FirstName> 
      <LastName></LastName> 
     </Customer> 
     <RepairFacility> 
      <LocationName></LocationName> 
      <LocationPhone></LocationPhone> 
     </RepairFacility> 
     <RepairFacility> 
      <LocationName></LocationName> 
      <LocationPhone></LocationPhone> 
     </RepairFacility> 
     <Action> 
      <FollowUpFlag></FollowUpFlag> 
      <DateAction></DateAction> 
     </Action> 
     <Action> 
      <FollowUpFlag></FollowUpFlag> 
      <DateAction></DateAction> 
     </Action> 
    </Element> 
    <Element> 
</Elements> 

Je suis reconnaissant pour toute aide.

Répondre

7

Essayez ceci:

-- Query 
SELECT (
    SELECT A1.FirstName 
     , A1.LastName 
    FROM #Customer AS A1 
    WHERE A1.ID = Z.CustomerID 
    FOR XML PATH('Customer') , TYPE 
), 
     (
    SELECT A2.LocationName 
     , A2.LocationPhone 
    FROM #RepairFacility AS A2 
    WHERE A2.ID = Z.RepairFacilityID 
    FOR XML PATH('RepairFacility') , TYPE 
), 
     (
    SELECT A3.FollowUpFlag 
     , A3.DateAction 
    FROM #Action AS A3 
    WHERE A3.ID = Z.ActionID 
    FOR XML PATH('Action') , TYPE 
) 
FROM (
    SELECT A1.ID AS CustomerID 
     , A2.ID AS RepairFacilityID 
     , A3.ID AS ActionID 
    FROM #Action A3 
     JOIN #Customer A1 ON (A3.CustomerID = A1.ID) 
     JOIN #RepairFacility A2 ON (A3.RepairFacilityID = A2.ID) 
) AS Z 
FOR XML PATH('Element'), ROOT('Elements') 

Voici un peu de données d'échantillon:

-- Sample data 
CREATE TABLE #Customer (
     ID    int    IDENTITY 
    , FirstName  varchar(50) 
    , LastName  varchar(50) 
) 

CREATE TABLE #RepairFacility (
     ID    int    IDENTITY 
    , LocationName varchar(50) 
    , LocationPhone varchar(50) 
) 

CREATE TABLE #Action (
     ID     int   IDENTITY 
    , CustomerID   int 
    , RepairFacilityID int 
    , FollowUpFlag  bit 
    , DateAction   datetime 
) 

INSERT #Customer (FirstName, LastName) VALUES ('John', 'Smith') 
INSERT #RepairFacility (LocationName, LocationPhone) VALUES ('New York', '(123) 555-1234') 
INSERT #Action (CustomerID, RepairFacilityID, FollowUpFlag, DateAction) VALUES (1, 1, 0, GETDATE()) 

INSERT #Customer (FirstName, LastName) VALUES ('Jane', 'Doe') 
INSERT #RepairFacility (LocationName, LocationPhone) VALUES ('Chicago', '(789) 555-7890') 
INSERT #Action (CustomerID, RepairFacilityID, FollowUpFlag, DateAction) VALUES (2, 2, 1, GETDATE()) 

Cette requête a la sortie suivante:

<Elements> 
    <Element> 
    <Customer> 
     <FirstName>John</FirstName> 
     <LastName>Smith</LastName> 
    </Customer> 
    <RepairFacility> 
     <LocationName>New York</LocationName> 
     <LocationPhone>(123) 555-1234</LocationPhone> 
    </RepairFacility> 
    <Action> 
     <FollowUpFlag>0</FollowUpFlag> 
     <DateAction>2012-03-22T08:33:08.617</DateAction> 
    </Action> 
    </Element> 
    <Element> 
    <Customer> 
     <FirstName>Jane</FirstName> 
     <LastName>Doe</LastName> 
    </Customer> 
    <RepairFacility> 
     <LocationName>Chicago</LocationName> 
     <LocationPhone>(789) 555-7890</LocationPhone> 
    </RepairFacility> 
    <Action> 
     <FollowUpFlag>1</FollowUpFlag> 
     <DateAction>2012-03-22T08:41:35.640</DateAction> 
    </Action> 
    </Element> 
</Elements> 
+0

Works! Merci Ryan –

Questions connexes