1

i have a table named 'Words' in sql server with these columns: Id, Type, Word

i want to write a query to generate a xml like this

<Words>
  <Word Id="1" Type="8">some text1</Word>
  <Word Id="2" Type="4">some text2</Word>
  <Word Id="3" Type="5">some text3</Word>
</Words>

how can i do that?

1 Answer 1

3
WITH Words(Id, Type, Word) AS
(
SELECT 1, 8, 'some text1' UNION ALL
SELECT 2, 4, 'some text2' UNION ALL
SELECT 3, 5, 'some text3' 
)
SELECT Id   AS [@Id],
       Type AS [@Type],
       Word AS [text()]
FROM Words
FOR XML PATH('Word'), ROOT('Words')
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.