In the previous post we have seen how to insert an attribute to an XML variable. In this post, let us see how to insert an element to an XML variable. In the example given below, an element named "LastName" is added to the "Employee" element.
DECLARE @x XMLSELECT @x = '<Employee> <FirstName>Jacob</FirstName></Employee>'DECLARE @LastName VARCHAR(15)SELECT @LastName = 'Sebastian'-- insert an attributeSET @x.modify(' insert element LastName {sql:variable("@LastName")} as last into (/Employee)[1]')-- test the resultsSELECT @x/*output:<Employee> <FirstName>Jacob</FirstName> <LastName>Sebastian</LastName></Employee>*/
Tags: XQuery-Labs, XML, XQuery, XQuery Functions, SQL Server XQuery, XQuery in TSQL, XQuery Training, XQuery Tutorial,