MonkeyKing
MonkeyKing

Reputation: 3

SQL Results to XML

When I run the below query:

SELECT c.FirstName
FROM HumanResources.Employee e
INNER JOIN Person.Contact c 
    ON c.ContactID = e.ContactID
WHERE c.FirstName = 'Rob'
FOR XML RAW ('Employee');

I get the following result:

<Employee FirstName="Rob"/>

I'm wondering is there a way that I can format it like below:

<Employee>Rob<Employee/>

Upvotes: 0

Views: 55

Answers (1)

Dale K
Dale K

Reputation: 27202

Use FOR XML PATH instead of FOR XML RAW reference

SELECT c.FirstName Employee
FROM (
    VALUES ('Rob'), ('Bob')
) c (FirstName)
FOR XML PATH ('');

Returns:

Result
<Employee>Rob</Employee><Employee>Bob</Employee>

Upvotes: 1

Related Questions