Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TSQL Updating XML Variable 1

Status
Not open for further replies.

T0AD

Programmer
Jun 4, 2003
73
GB
Hello.

I have the following code in a SQL trigger:
Code:
SELECT @XML = SELECT * FROM inserted FOR XML PATH ('MyEntity'), ELEMENTS, ROOT('Inserted'), TYPE);

I want to add the deleted records to this too, but my code below just overwrites the @XML variable. How do I add to it?

Code:
SELECT @XML = SELECT * FROM deleted FOR XML PATH ('MyEntity'), ELEMENTS, ROOT('Deleted'), TYPE);

Many thanks in advance,
Pete

There's a thin line between genius, and insanity!
 
You could cast each return value to string, concatenate and then cast it back to xml:
Code:
SELECT @XML =
CAST(CAST((SELECT * FROM inserted FOR XML PATH ('MyEntity'), ELEMENTS, ROOT('Inserted'), TYPE) AS varchar(max))
+ CAST((SELECT * FROM deleted FOR XML PATH ('MyEntity'), ELEMENTS, ROOT('Deleted'), TYPE) AS varchar(max))
 AS xml);
 
Didn't think of that, thanks!

There's a thin line between genius, and insanity!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top