设置 XML 列动态方式

huangapple go评论67阅读模式
英文:

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:

  1. Filter by all the records that have the same value of AccountNumber and SourceAccountNumber
  2. I save in a temporary table the PK (biCustNoteID) of the table and a biAccountID (which is the same number of the value AccountNumber)
  3. 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.

huangapple
  • 本文由 发表于 2023年2月24日 07:04:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定