There might be valid reason to store XML in a [n]varchar(max)
. If you want to only store the XML it is perfectly OK but if you want to modify parts of the XML using TSQL or if you need to query the XML for values or use node/attribute values in a where clause you should switch to XML where you can benefit from indexes on the data and skip the type conversions. Since text
is deprecated you should at least consider to switch data type to [n]varchar(max)
If you had your data in a XML column you would use XML DML to modify the XML. In your case you would use replace value of like this.
update tbl_Module_RequestForms_Items
set XMLData.modify('replace value of (/Record/Submitted/text())[1] with "True"')
where XMLData.value('(/Record/Submitted)[1]', 'bit') = 0
Without the XML data type that is not possible so you have to extract the entire XML document, modify it and then update the table with the modified XML document.
You can of course do that using some kind of client development tool but it is also possible in TSQL.
- Declare a table variable with the primary key from
tbl_Module_RequestForms_Items
and the XMLData column but as data type XML.
- Copy the rows from
tbl_Module_RequestForms_Items
to the table variable that should be updated.
- Update the XML using
replace value of
.
- Apply the changes back to
tbl_Module_RequestForms_Items
.
Something like this where I assume that ID
is the primary key for tbl_Module_RequestForms_Items
and that your XML data is in column XMLData
:
declare @T table
(
ID int primary key,
XMLData xml
)
insert into @T
select M.ID,
M.XMLData
from tbl_Module_RequestForms_Items as M
where cast(cast(XMLData as nvarchar(max)) as xml).value('(/Record/Submitted)[1]', 'bit') = 0
update @T
set XMLData.modify('replace value of (/Record/Submitted/text())[1] with "True"')
update M
set XMLData = cast(T.XMLData as nvarchar(max))
from tbl_Module_RequestForms_Items as M
inner join @T as T
on M.ID = T.ID