Actually, I donβt know what (and why) you are doing it. It seems rather complicated ...
You can solve the error that occurs:
Just paste this into an empty request window and pass the idea into your code. The point is the second DECLARE @genderVal that is commented out. Change the comments to another DECLARE and you can play it. If this value is NULL, you will receive an error message. This means: check the value of the variable. It seems to be NULL.
Remember that I fixed the invalid closing Phys tags and I removed the internal root-tags ...
declare @xml XML= '<root> <Physicians> <name>Doctor1</name> <gender>M</gender> <langF1> <a title="English" href="/default.aspx?id=23">English</a> </langF1> </Physicians> <Physicians> <name>Doctor2</name> <gender>F</gender> <langF1> <a title="French" href="/default.aspx?id=56">French</a> </langF1> </Physicians> </root>'; select @xml; --This will search for the physician with the name "Doctor1"... Dont' know if you need this... Your example could be meant with one XML-for each physician... DECLARE @ProviderName varchar(200)='Doctor1'; --DECLARE @genderVal varchar(10)=NULL; DECLARE @genderVal varchar(10)='F'; --Update Doctor1 with new @genderVal=NULL --> The error you described SET @xml.modify('replace value of (/root/Physicians[name=sql:variable("@ProviderName")]/gender/text())[1] with sql:variable("@genderVal")'); select @xml;
EDIT: change the language
If I understand you correctly, you donβt need to worry if the language exists or not. You just delete it anyway
SET @xml.modify('delete (/root/Physicians[name=sql:variable("@ProviderName")]/langF1/a)[1]'); select @xml;
Now you create your internal xml for "a":
DECLARE @langTitle varchar(100)='French'; DECLARE @langLink varchar(100)='/default.aspx?id=56'; DECLARE @newLang XML='<a title="' + @langTitle + '" href="' + @langLink + '">' + @langTitle + '</a>';
This internal xml can be inserted into an existing node:
SET @xml.modify('insert sql:variable("@newLang") into (/root/Physicians[name=sql:variable("@ProviderName")]/langF1)[1]'); select @xml;
source share