0

I have this XML:

<Files>
  <File>
    <Name>pic1</Name> 
    <Description>Pics</Description> 
    <URL>URL1</URL> 
    <TypeText>jpg</TypeText> 
  </File>
  <File>
    <Name>pic2</Name> 
    <Description>Pics</Description> 
    <URL>URL2</URL> 
    <TypeText>jpg</TypeText> 
  </File>
  <File>
    <Name>pic3</Name> 
    <Description>Pics</Description> 
    <URL>URL3</URL> 
    <TypeText>jpg</TypeText> 
  </File>
</Files>

And I want to insert them in an SQL Server table. I use this code:

INSERT INTO #T
SELECT
    XFiles.value('/Name', 'nvarchar(120)'),
    XFiles.value('/Description', 'nvarchar(300)'),
    XFiles.value('/URL', 'nvarchar(max)'),
    XFiles.value('/TypeText', 'nvarchar(120)')
FROM @FilesXML.nodes('/Files/File')
AS XTbl(XFiles)

but it gives me error.

XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'

I think I'm making a mistake with the jpath part. Is it right or wrong?

Thanx in advance

2
  • 1
    What is the error? You should provide the error here. Commented Jul 29, 2014 at 11:11
  • @Ivica Sorry, Post Updated :) Commented Jul 29, 2014 at 11:20

1 Answer 1

3

You need to alter your select query :

SELECT
    XFiles.value('Name[1]', 'nvarchar(120)') as Name,
    XFiles.value('Description[1]', 'nvarchar(300)'),
    XFiles.value('URL[1]', 'nvarchar(max)'),
    XFiles.value('TypeText[1]', 'nvarchar(120)')
FROM @FilesXML.nodes('/Files/File') as XTbl(XFiles)

If you just select Name, for example, that XPath query returns a collection of Name nodes - even if there's only one child node! - hence the error you're seeing. You have to ensure that the XPath query returns only a single thing, hence adding the [1] indexer.

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

Comments

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.