英文:
Set XML column dynamically
问题
以下是代码部分的翻译:
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
希望这有所帮助。
英文:
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:
- Filter by all the records that have the same value of
AccountNumber
andSourceAccountNumber
- I save in a temporary table the PK (
biCustNoteID
) of the table and abiAccountID
(which is the same number of the valueAccountNumber
) - Inside the loop I look in another table with the
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.
答案1
得分: 1
你在更新中遗漏了XPath的一部分,必须使用sql:variable
函数引用TSQL本地变量。例如:
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
由于目标节点都不位于命名空间中,所以无需声明XML命名空间。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论