I want to modify a value inside an XML tag dynamically, the stored procedure runs correctly but does not modify the value.
This is the query:
DROP TABLE IF EXISTS #IDsToUpdate
SELECT biCustNoteID, biAccountID
INTO #IDsToUpdate
FROM tbCustNote
WHERE vcParams.value('(/ArrayOfNoteParameterDC/NoteParameterDC[ParameterEnum = ''AccountNumber'']/Value/text())[1]', 'varchar(max)') =
vcParams.value('(/ArrayOfNoteParameterDC/NoteParameterDC[ParameterEnum = ''SourceAccountNumber'']/Value/text())[1]', 'varchar(max)')
WHILE EXISTS (SELECT * FROM #IDsToUpdate)
BEGIN
DECLARE @Id bigint
SET @Id = (SELECT TOP 1 biCustNoteID FROM #IDsToUpdate);
DECLARE @biCustAccountID bigint
SET @biCustAccountID = (SELECT TOP 1 biAccountID FROM #IDsToUpdate WHERE biCustNoteID = @Id);
DECLARE @vcCustAccountID bigint
SET @vcCustAccountID = (SELECT TOP 1 vcAccountID FROM tbcustaccount WHERE biaccountid = @biCustAccountID);
WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' as xsi, 'http://www.w3.org/2001/XMLSchema' as xsd)
UPDATE tbCustNote
SET vcParams.modify('replace value of (/ArrayOfNoteParameterDC[ParameterEnum = ''AccountNumber'']/Value/text())[1] with "@vcCustAccountID"')
WHERE biCustNoteID = @Id
DELETE #IDsToUpdate WHERE biCustNoteID = @Id
END
And this is the XML:
<ArrayOfNoteParameterDC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<NoteParameterDC>
<ParameterEnum>LicensePlate</ParameterEnum>
<Value>100299</Value>
<ParameterName>LicensePlate</ParameterName>
</NoteParameterDC>
<NoteParameterDC>
<ParameterEnum>Vehicle</ParameterEnum>
<Value>Unknown</Value>
<ParameterName>Vehicle</ParameterName>
</NoteParameterDC>
<NoteParameterDC>
<ParameterEnum>AccountNumber</ParameterEnum>
<Value>543152065</Value>
<ParameterName>AccountNumber</ParameterName>
</NoteParameterDC>
<NoteParameterDC>
<ParameterEnum>SourceAccountNumber</ParameterEnum>
<Value>543152065</Value>
<ParameterName>SourceAccountNumber</ParameterName>
</NoteParameterDC>
<NoteParameterDC>
<ParameterEnum>CustomerVehicleId</ParameterEnum>
<Value>27104593</Value>
<ParameterName>CustomerVehicleId</ParameterName>
</NoteParameterDC>
</ArrayOfNoteParameterDC>
I want to modify the value of "AccountNumber"
<NoteParameterDC>
<ParameterEnum>AccountNumber</ParameterEnum>
<Value>543152065</Value>
<ParameterName>AccountNumber</ParameterName>
</NoteParameterDC>
What I'm trying to do in the code is the following:
AccountNumber
and SourceAccountNumber
biCustNoteID
) of the table and a biAccountID
(which is the same number of the value AccountNumber
)biAccountID
for the ID that I use to modify the XML.I'm not sure if the XML can be modified the way I'm currently doing it, I couldn't find documentation about it. If you have any suggestions I'd appreciate it.
You've just left out part of the XPath in your update, and you must use the sql:variable
function to reference a TSQL local variable. EG
declare @vcCustAccountID int = 12;
UPDATE tbCustNote
set vcParams.modify('replace value of (/ArrayOfNoteParameterDC/NoteParameterDC[ParameterEnum = ''AccountNumber'']/Value/text())[1] with sql:variable("@vcCustAccountID")')
where biCustNoteID = 1
There's no need to declare the XML namespaces since none of your target nodes are actually in those namespaces.