I am attempting to insert into some xml I am retrieving from a column in a table. I retrieve it as follows:
DECLARE @profiles_xml xml
DECLARE @profile_id int
SET @profile_id = 16
SET @profiles_xml = (SELECT profiles from tbl_applied_profiles WHERE
profiles.value('(Profile/ID)[1]','int')= @profile_id)
The resulting xml looks like this:
<Profile>
<ID>16</ID>
<User>
<ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID>
<Name>test</Name>
<Activities />
</User>
</Profile>
However when I try and insert an element into it like this:
SET @devices_xml.modify('insert <Activity><Name>testname</Name></Activity>
as first into (Profiles/User/Activities[1])')
I get the following error:
XQuery [modify()]: The target of 'insert' must be a single node, found 'element(Activities,xdt:untyped) *'
I don't understand why there is an error, as there is only one Activities element currently.
You have two little errors:
<Profile> - but you had Profiles in your insert XQuery[1] indexer in the wrong place (inside the closing ) ).Try this XQuery:
SET @devices_xml.modify('insert <Activity><Name>testname</Name></Activity>
as first into (Profile/User/Activities)[1]')
At least in my case, this then returned:
<Profile>
<ID>16</ID>
<User>
<ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID>
<Name>test</Name>
<Activities>
<Activity>
<Name>testname</Name>
</Activity>
</Activities>
</User>
</Profile>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With