I would like to use a stored procedure to insert some values passed as parameters into elements in an xml column. I still have it. The following options:
@profile_id int, @user_id nvarchar(50), @activity_name nvarchar(50), @display_name nvarchar(50)
Get the desired xml:
DECLARE @profiles_xml xml SET @profiles_xml = (SELECT profiles from tbl_applied_profiles WHERE profiles.value('(Profile/ID)[1]','int')= @profile_id)
The xml from the column inside @profiles_xml is as follows:
<Profile> <ID>20</ID> <User> <ID>BC4A18CA-AFB5-4268-BDA9-C990DAFE7783</ID> <Name>somename</Name> <Activities> <Activity> <Name>activity1</Name> </Activity> </Activities> </User> </Profile>
Trying to insert a user name and display name into a user with a specific identifier:
SET @profiles_xml.modify(' insert if(/Profile/User/ID=sql:variable("@user_id")) then Activity[Name=sql:variable("@activity_name")][DisplayName=sql:variable("@display_name")] else() as first into (/Profile/User/Activities)[1]')
I also tried this without success:
SET @devices_xml.modify(' insert /Profile/User[ID=sql:variable("@user_id")]/Activity[Name=sql:variable("@activity_name")][DisplayName=sql:variable("@display_name")] into (/Profile/User/Activities)[1]')
And this:
SET @devices_xml.modify(' insert /Profile/User[ID=sql:variable("@user_id")]/Activities/Activity[Name=sql:variable("@activity_name")][DisplayName=sql:variable("@display_name")] into (/Profile/User/Activities)[1]')
What is the right way to do this?
Christian
source share