简体   繁体   中英

Can't set SQL XML node level with <xsi:type=> in path

I am attempting to modify a XML data type field with the SQL update statement below. I am having trouble setting the correct path past the level /Operand xsi:type="QueryObjectKey"/ to reach the /Name/ field. I can set the path to modify parent fields/values like /GroupBegin/false/GroupBegin/ with no problem, so I know the SQL statement is correct, just the path syntax to reach /Name/ is not right.

I get this ambiguous error message

Msg 2205, Level 16, State 1, Line 6
XQuery [ACVSCore.Access.Query.XMLEncodedCriteria.modify()]: ")" was expected.

How do I set that path to include the / Operand xsi:type="QueryObjectKey" /

DECLARE @NewValue Varchar(255) =  'None'

update
[ACVSCore].[Access].[Query]
SET
    XMLEncodedCriteria.modify('replace value of 
    (/QueryExpression/Criteria/CriteriaExpression/Operand xsi:type="QueryObjectKey"/Index/text())[1] with sql:variable("@NewValue")')
<QueryExpression xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" PrimaryObjectType="SoftwareHouse.NextGen.Common.SecurityObjects.Personnel" DataFetcherType="SoftwareHouse.CrossFire.Common.Objects.DataQuery">
  <DataFetcherTag xsi:type="xsd:string" />
  <Criteria>
    <CriteriaExpression>
      <GroupBegin>false</GroupBegin>
      <Operand xsi:type="QueryObjectKey">
        <Name>ORIGINAL</Name>
      </Operand>
    </CriteriaExpression>
  </Criteria>
</QueryExpression>

Your XML is using a namespace without its declaration. First, you need to fix the XML, and after that modify the desired XML element value. For the reference: What is the difference between xsd and xsi?

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, xml_data XML);
INSERT INTO @tbl (xml_data) 
VALUES ('<QueryExpression xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 PrimaryObjectType="SoftwareHouse.NextGen.Common.SecurityObjects.Personnel"
                 DataFetcherType="SoftwareHouse.CrossFire.Common.Objects.DataQuery">
    <DataFetcherTag xsi:type="xsd:string"/>
    <Criteria>
        <CriteriaExpression>
            <GroupBegin>false</GroupBegin>
            <Operand xsi:type="QueryObjectKey">
                <Name>ORIGINAL</Name>
            </Operand>
        </CriteriaExpression>
    </Criteria>
</QueryExpression>');
-- DDL and sample data population, end

-- before
SELECT * FROM @tbl;

DECLARE @NewValue VARCHAR(10) = 'None';
UPDATE @tbl
SET xml_data.modify('replace value of 
(/QueryExpression/Criteria/CriteriaExpression/Operand/Name/text())[1] with (sql:variable("@NewValue"))');

-- after
SELECT * FROM @tbl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM