1

I am trying to import an xml file into the sql server with no success yet.
The xml file is structured like this:

<Users xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  <User>
    <PartitionKey>be-BY</PartitionKey>
  </User>
</Users>  

I am using the following code:

   SELECT 
    xmldata.value('(PartitionKey)[1]', 'NCHAR(10)') AS 'partition_key'  
FROM 
    (SELECT CAST(x AS XML)
     FROM OPENROWSET(
        BULK 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\SkillageXML\userstest1111.xml',
        SINGLE_BLOB
        ) 
        AS T(x)
    )  AS T(x)
CROSS APPLY 
    x.nodes('/Users/User') AS X(xmldata);

However, I don't see any value after it is done with processing the file. Is there anything missing?

1 Answer 1

6

This works like a charm for me:

SELECT 
    XUsers.value('(PartitionKey)[1]', 'NCHAR(10)') AS 'partition_key'  
FROM 
    (SELECT 
        BulkXML = CAST(BulkColumn AS XML)
     FROM 
        OPENROWSET(BULK 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\SkillageXML\userstest1111.xml', SINGLE_BLOB) AS BX 
    ) AS T
CROSS APPLY 
    BulkXml.nodes('/Users/User') AS XTbl(XUsers);and returns:

partition_key
be-BY     

I think you're approach of using the AS T(x) alias twice is causing confusion - try to use something more meaningful and not the same alias for both things.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.