简体   繁体   中英

Import XML using SQL Server, the problem is it is returning only one record at a time and I want all records in one go

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = 
N'<ROOT>
   <B2A>
    <B2A.01>
     <B2A.01.1>00</B2A.01.1>
    </B2A.01>
    <B2A.02>
     <B2A.02.1>LT</B2A.02.1>
    </B2A.02>
   </B2A>
   <L11>
    <L11.01>
     <L11.01.1>1123</L11.01.1>
    </L11.01>
   </L11>
   <L11>
    <L11.01>
     <L11.01.1>120001502140</L11.01.1>
    </L11.01>
    <L11.02>
     <L11.02.1>BN</L11.02.1>
    </L11.02>
   </L11>
   <L11>
    <L11.01>
     <L11.01.1>KDC</L11.01.1>
    </L11.01>
    <L11.02>
     <L11.02.1>11</L11.02.1>
    </L11.02>
    <L11.03>
     <L11.03.1>11</L11.03.1>
    </L11.03>
   </L11>
 </ROOT>'

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

My approach

SELECT 
T1.be.value ('(B2A/B2A.01/B2A.01.1)[1]' ,'nvarchar(4)')  as [tcode],
T1.be.value ('(B2A/B2A.02/B2A.02.1)[1]' ,'nvarchar(4)')  as [apptype],
T.te.value ('(L11/L11.01/L11.01.1)[1]' ,'nvarchar(60)')  as [id],
T.te.value ('(L11/L11.02/L11.02.1)[1]' ,'nvarchar(6)')   as [Ref id],
T.te.value ('(L11/L11.03/L11.03.1)[1]' ,'nvarchar(160)') as [DESCRIPTION]
FROM    (
    SELECT  @xml AS x
    ) xml  
x.nodes('/ROOT/L11') T(te)
outer apply x.nodes('/ROOT/B2A') T1(be)

But here the problem is it is giving only one record at a time and I want all records in one go.

This is what I am getting

tcode apptype id Ref id DESCRIPTION
00 LT null null null

and if there are multiple records then it should show like this

tcode apptype id Ref id DESCRIPTION
00 LT 1123 null null
00 LT 120001502140 BN null
00 LT KDC 11 11

Try this:

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = N'<ROOT><L11><L11.01><L11.01.1>1123</L11.01.1></L11.01></L11><L11><L11.01><L11.01.1>120001502140</L11.01.1></L11.01><L11.02><L11.02.1>BN</L11.02.1></L11.02></L11><L11><L11.01><L11.01.1>KDC</L11.01.1></L11.01><L11.02><L11.02.1>11</L11.02.1></L11.02><L11.03><L11.03.1>11</L11.03.1></L11.03></L11></ROOT>';


SELECT ROW_NUMBER() OVER (ORDER BY T.c)
      ,T.c.query('.')  
      ,T.c.value ('(./L11.01/L11.01.1)[1]' ,'nvarchar(60)')  as [id]
      ,T.c.value ('(./L11.02/L11.02.1)[1]' ,'nvarchar(6)')   as [Ref id]
      ,T.c.value ('(./L11.03/L11.03.1)[1]' ,'nvarchar(160)') as [DESCRIPTION]
FROM @XML.nodes('ROOT/L11') T(c);

在此处输入图像描述

And for your edit, you can get the data like the following:

 SELECT T.c.value ('(./B2A.01/B2A.01.1)[1]' ,'nvarchar(60)')  as [tcode]
       ,T.c.value ('(./B2A.02/B2A.02.1)[1]' ,'nvarchar(6)')   as [apptype]
       ,T.c.value ('(./L11.01/L11.01.1)[1]' ,'nvarchar(60)')  as [id]
       ,T.c.value ('(./L11.02/L11.02.1)[1]' ,'nvarchar(6)')   as [Ref id]
       ,T.c.value ('(./L11.03/L11.03.1)[1]' ,'nvarchar(160)') as [DESCRIPTION]
FROM @XML.nodes('ROOT/*') T(c)

and then to use T-SQL to shape it the way you want. For example:

 WITH DataSource AS
 (
     SELECT T.c.value ('(./B2A.01/B2A.01.1)[1]' ,'nvarchar(60)')  as [tcode]
           ,T.c.value ('(./B2A.02/B2A.02.1)[1]' ,'nvarchar(6)')   as [apptype]
           ,T.c.value ('(./L11.01/L11.01.1)[1]' ,'nvarchar(60)')  as [id]
           ,T.c.value ('(./L11.02/L11.02.1)[1]' ,'nvarchar(6)')   as [Ref id]
           ,T.c.value ('(./L11.03/L11.03.1)[1]' ,'nvarchar(160)') as [DESCRIPTION]
    FROM @XML.nodes('ROOT/*') T(c)
)
SELECT MAX([tcode]) OVER()
      ,MAX([apptype]) OVER()
      ,[id]
      ,[Ref id]
      ,[DESCRIPTION]
FROM DataSource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM