2009-06-11 5 views
0

Ich versuche herauszufinden, Daten aus einer Tabelle in XML-Datei zu exportieren. Hier ist die Situationsql und xml

Table1 
contractID  subcontractid  ContractName  ContractType 
123        John    Electrical 
123   1     John    Comercial 
123   2     John    Resident 
134        Jim    Plumping 
134   1     Jim    Comercial 
134   2     Jim    Resindent 

Ich mag würde eine XML-Datei ausgegeben haben, wie folgend]

<contracts> 
    <contract contractid = 123 contractname = john, contracttype = Electrical> 
    <subcontract subcontractid = 1, contractname = John, contractType = Comercial /> 
    <subcontract subcontractid = 2, contractname = John, contractType = Resident /> 
    </contract> 
    <contract contractid = 134 contractname = Jim, contracttype = Plumping> 
    <subcontract subcontractid = 1, contractname = Jim, contractType = Comercial /> 
    <subcontract subcontractid = 2, contractname = Jim, contractType = Resident /> 
    </contract> 
</contracts> 

Dank für alles hilft.

+0

Welche Marke von SQL? –

+0

Was ist Ihre SQL-Engine ??? – Pierre

Antwort

0

Der spezifische Datenbank-Geschmack wird einen großen Unterschied in den verfügbaren Optionen machen.

SQL Server, zum Beispiel, können Sie eine "FOR XML" -Klausel an das Ende einer Abfrage anhängen, aber Oracle, MySQL und andere DBs erfordern unterschiedliche Ansätze.

1

Für SQL Server 2005/2008 Sie so etwas wie tun würde:

SELECT 
    contractID as [@contractid], 
    ContractName as [@contractname], 
    ContractType As [@contracttype], 
    (SELECT 
     subcontractid as [@subcontractid], 
     ContractName as [@contractname], 
     ContractType As [@contracttype] 
     FROM Table1 AS inner 
     WHERE outer.contractID=inner.contractID AND 
      subcontractid IS NOT NULL FOR XML PATH('subcontract'), TYPE) as [node()] 
    WHERE subcontractid IS NULL 
FOR XML PATH('contract'), ROOT('contracts') 

Für Oracle: So etwas wie:

SELECT XMLELEMENT('contracts', 
    (SELECT XMLELMENT('contract', 
     XMLAgg(
      XMLAttributes(
       contractID as 'contractid', 
       ContractName as 'contractname', 
       ContractType as 'contracttype'), 
      (SELECT XMLElement('subcontract', 
       XMLAgg(
        XMLAttributes(
         subcontractid as 'subcontractid', 
         ContractName as 'contractname', 
         ContractType as 'contracttype' 
        ) 
       ) 
      FROM Table1 AS inner 
      WHERE inner.contractID=outer.contractID AND subcontractid IS NOT NULL 
      ) 
     ) 
    ) 
    FROM Table1 AS outer WHERE subcontractid IS NULL 
    ) 
) 
+0

Eine weitere Frage bitte. Kann ich das Ergebnis trotzdem in der Datei speichern? Danke. – user79127

2

Meine Güte, geschlagen von 50 Sekunden! Ich werde Sie lambacck für die Geschwindigkeit aufladen :-)

SELECT co.contractid AS '@contractid', 
    co.contractname AS '@contractname', 
     co.contracttype AS '@contractType' 
     (SELECT sc.subcontractid AS '@subcontractid', 
     sc.contractname AS '@contractname', 
     sc.contractType AS '@contractType' 
     FROM contract sc 
    WHERE subcontractid IS NOT NULL AND sc.contractid = co.contractid 
    FOR XML PATH('subcontract'),Type) 
FROM Contract co 
WHERE subcontractid IS NULL 
FOR XML PATH('Contract'), ROOT('Contracts') 
+0

Funktioniert die innere Auswahl ohne AS [node()]? Als ich das vor ein paar Tagen gemacht habe, dachte ich, dass es notwendig ist. – lambacck

+0

Ich habe es gerade auf meiner SQL 2008-Instanz versucht und es funktionierte ohne. –

+0

+1 für Ihre Lösung tatsächlich testen :) – lambacck