2009-03-27 10 views
6

Je peux trouver beaucoup d'exemples sur la façon d'importer certains types de données XML dans SQL Server 2005. Mais on m'a donné des données dans le format suivant (en répétant "row" et "cell" avec ID au lieu des balises été nommé etc:Importation de XML dans SQL Server

<?xml version="1.0"?> <rows> 
    <row id='1'> 
     <cell id='category'>Simple</cell> 
     <cell id='query'>summary</cell> 
     <cell id='clientsfound'>6</cell> 
     <cell id='eligibleclients'>11</cell> 
     <cell id='percentage'>55</cell> 
     <cell id='days'>0</cell> 
    </row> 

    <row id='2'> 
     <cell id='category'>Complex</cell> 
     <cell id='query'>details</cell> 
     <cell id='clientsfound'>4</cell> 
     <cell id='eligibleclients'>6</cell> 
     <cell id='percentage'>67</cell> 
     <cell id='days'>5</cell> 
    </row> 

    ... 
    </rows> 

Idéalement je veux le charger dans un tableau tel que:

CREATE TABLE [dbo].[QueryResults](
    [UserString] [varchar](50) NULL, 
    [ImportStamp] [timestamp] NULL, 
    [RowID] [int] NULL, 
    [Category] [nchar](10) NULL, 
    [Query] [nchar](10) NULL, 
    [ClientsFound] [int] NULL, 
    [EligibleClients] [int] NULL, 
    [Percentage] [int] NULL, 
    [Days] [int] NULL 
) 

que quelqu'un peut me donner un exemple ou un point à vers un tutoriel en ligne

?

Répondre

1

Vous pouvez le faire en utilisant OPENXML et XQUERY.

DECLARE @XMLdoc XML 
DECLARE @idoc int 
SELECT @XMLdoc = '<?xml version="1.0"?> 
    <rows> 
    <row id="1"> 
     <cell id="category">Simple</cell> 
     <cell id="query">summary</cell> 
     <cell id="clientsfound">6</cell> 
     <cell id="eligibleclients">11</cell> 
     <cell id="percentage">55</cell> 
     <cell id="days">0</cell> 
    </row> 
    <row id="2"> 
     <cell id="category">Complex</cell> 
     <cell id="query">details</cell> 
     <cell id="clientsfound">4</cell> 
     <cell id="eligibleclients">6</cell> 
     <cell id="percentage">67</cell> 
     <cell id="days">5</cell> 
    </row> 
    </rows>' 


-- Create an internal representation of the XML document. 
EXEC sp_xml_preparedocument @idoc OUTPUT, @XMLDoc 

INSERT INTO QueryResults (RowID,Category,Query,ClientsFound,EligibleClients,Percentage,Days) 
SELECT id, 
     overflow.value('(/row/cell[@id="category"])[1]', 'nchar(10)'), 
     overflow.value('(/row/cell[@id="query"])[1]', 'nchar(10)'), 
     overflow.value('(/row/cell[@id="clientsfound"])[1]', 'int'), 
     overflow.value('(/row/cell[@id="eligibleclients"])[1]', 'int'), 
     overflow.value('(/row/cell[@id="percentage"])[1]', 'int'), 
     overflow.value('(/row/cell[@id="days"])[1]', 'int') 
FROM OPENXML (@idoc, '/rows/row',10) 
WITH (id int '@id', 
    overflow xml '@mp:xmltext' --the row xml node 
) 

-- Release resources allocated for the XML document. 
EXEC sp_xml_removedocument @idoc 

SELECT * FROM QueryResults 

Résultats:

UserString ImportStamp  RowID Category Query ClientsFound EligibleClients Percentage Days 
----------- ------------------ ------ --------- -------- ------------ --------------- ----------- ---- 
NULL  0x000000000000C1CA 1  Simple summary 6   11    55   0 
NULL  0x000000000000C1CB 2  Complex details 4   6    67   5 

Je ne suis pas sûr de ce que vous voulez peuplé 'UserString', mais vous pouvez régler cela plus tard.

Espérons que ceci fournit une solution appropriée à votre question.

Désolé, vous avez probablement raison à propos de sp_xml_preparedocument. Je viens de prendre cette approche à partir de procs stockés similaires que nous avions sur un projet que nous avons travaillé avec l'équipe de Microsoft SDC, donc pensé que ce serait sûr. Votre approche est probablement plus propre de toute façon.

+0

Pas besoin d'utiliser sp_xml_preparedocument dans SQL 2005 – gbn

+0

changements de traitement XML sont l'une des meilleures fonctionnalités de SQL Server 2005 ... :-) – gbn

9

Le XML ne devrait pas être "" interne, non? De toute façon, vous pouvez analyser nativement le type de données XML. sp_xml_preparedocument est franchement dangereux en raison du surcoût de l'utilisation de la mémoire.

DECLARE @foo XML; 

SET @foo = N'<?xml version="1.0"?> 
<rows> 
    <row id="1"> 
     <cell id="category">Simple</cell> 
     <cell id="query">summary</cell> 
     <cell id="clientsfound">6</cell> 
     <cell id="eligibleclients">11</cell> 
     <cell id="percentage">55</cell> 
     <cell id="days">0</cell> 
    </row> 
    <row id="2"> 
     <cell id="category">Complex</cell> 
     <cell id="query">details</cell> 
     <cell id="clientsfound">4</cell> 
     <cell id="eligibleclients">6</cell> 
     <cell id="percentage">67</cell> 
     <cell id="days">5</cell> 
    </row> 
</rows>'; 

SELECT 
    x.item.value('@id', 'int') AS RowID, 
    y.item.value('(./cell[@id="category"])[1]', 'nchar(10)') AS category, 
    y.item.value('(./cell[@id="query"])[1]', 'nchar(10)') AS query, 
    y.item.value('(./cell[@id="clientsfound"])[1]', 'int') AS clientsfound, 
    y.item.value('(./cell[@id="eligibleclients"])[1]', 'int') AS eligibleclients, 
    y.item.value('(./cell[@id="percentage"])[1]', 'int') AS percentage, 
    y.item.value('(./cell[@id="days"])[1]', 'int') AS days 
FROM 
    @foo.nodes('/rows/row') x(item) 
    CROSS APPLY 
    x.item.nodes('.') AS y(item) 
Questions connexes