Is it possible to update a table and return the updated values in a FOR XML statement?
Consider the following table of customers:
Customers
Id FirstNam LastNam
1 John Doe
2 James Smith
3 Martin Jones
I’d like to update the FirstNam of the customer with Id 2 to David, and return the result as XML.
I’ve tried using:
UPDATE Customers
SET FirstNam = 'David'
WHERE Id = 2
OUTPUT inserted.FirstNam
FOR XML PATH('')
which, of course, doesn’t work. What am I doing wrong? As always, any help is greatly appreciated!
We can not insert the updated values to XML directly.
If you want to save XML of updated data, then you need to make it in two steps:
Step 1: Use Table Variable and insert the updated data into that table variable (as simple process)
Step 2:
Generate XML from table variable @Updated as:
and save it to the place where you want.