1

I have a XML column in a table with values like

<m lingua="1">  
  <coloriVini>  
    <i n="8" />  
    <i n="2" />  
    <i n="3" />  
    <i n="4" />  
    <i n="5" />  
    <i n="6" />  
    <i n="7" />  
  </coloriVini>  
</m>

and I need to make a join with values from /m/coloriVini/i/@n with another table with will be displayed comma separated , but this I can handle

One idea is to make a variable and store the XML, but I am asking you if I can do it easily

I've tried

SELECT [L].[nome]
, p.value('./coloriVini', 'xml')
FROM [dbo].[contatto] C
CROSS APPLY [xmlMailing].nodes('m') t(p)
LEFT JOIN [dbo].[lingua] L ON p.value('./@lingua', 'int') = L.id

I know p.value('./coloriVini', 'xml') or p.value('./coloriVini', 'nvarchar(200)') doesn't work, can you tell me what I'm doing wrong
Thanks

1 Answer 1

1

Try this one -

DECLARE @XML XML
SELECT @XML = 
'<m lingua="1">  
  <coloriVini>  
    <i n="8" />  
    <i n="2" />  
    <i n="3" />  
    <i n="4" />  
    <i n="5" />  
    <i n="6" />  
    <i n="7" />  
  </coloriVini>  
</m>'

DECLARE @lingua TABLE
(
      id INT
    , nome CHAR(1)
)

INSERT INTO @lingua (id, nome)
VALUES 
    (8, '8'),
    (2, '2')

SELECT 
      l.nome
    , x.coloriVini
FROM (
    SELECT 
          id = t.p.value('@n', 'int') 
        , coloriVini = t.p.query('../.')
    FROM @XML.nodes('/m/coloriVini/i') t(p)
) x
JOIN @lingua l ON l.id = x.id
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.