代码之家  ›  专栏  ›  技术社区  ›  sikrut

UPDATEXML抛出SQL错误:ORA-00927:尝试转换和更新CLOB数据类型时缺少等号

  •  0
  • sikrut  · 技术社区  · 9 年前

    UPDATEXML 功能: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions205.htm

    现在,我正试图将其应用于数据库中的数据。我数据库中的列是 CLOB公司 SQL Error: ORA-00927: missing equal sign .


    企图

    使用数据库的数据类型重用Oracle示例。

    CREATE TABLE xwarehouses
    (  
      MSD_ID INTEGER,
      CDATA CLOB
    );
    

    INSERT INTO xwarehouses VALUES 
      (1,
      ('<?xml version="1.0"?>
       <Warehouse>
        <WarehouseId>1</WarehouseId>
        <WarehouseName>Southlake, Texas</WarehouseName>
        <Building>Owned</Building>
        <Area>25000</Area>
        <Docks>2</Docks>
        <DockType>Rear load</DockType>
        <WaterAccess>true</WaterAccess>
        <RailAccess>N</RailAccess>
        <Parking>Street</Parking>
        <VClearance>10</VClearance>
       </Warehouse>'));
    

    3.将CLOB转换为XMLTYPE以从指定标记中提取值

    SELECT EXTRACTVALUE(XMLTYPE(cdata),'/Warehouse/WaterAccess') as VAL from xwarehouses;
    

    VAL | true

    4.尝试使用更新中的相同转换更新值

    update XWAREHOUSES
    set XMLTYPE(cdata) = 
        updatexml(XMLTYPE(cdata),'/Warehouse/WaterAccess/text()','false')
    where msd_id = 1;
    

    2 回复  |  直到 6 年前
        1
  •  1
  •   sikrut    9 年前

    好的,我用两种不同的方法解决了这个问题,两种方法基本相同,但一种是查询,另一种是脚本。

    将数据加载为xmltype,然后执行xml操作,最后在执行更新之前转换回clob

    update xwarehouses
    set cdata =
    (
      select updatexml(xmltype(cdata),'/Warehouse/WaterAccess/text()','true').getclobval() 
      from xwarehouses where msd_id = 1
    )
    where msd_id = 1;
    commit;
    

    解决方案2

    这不是最整洁的版本,因为我可能会对来自 解决方案1

    DECLARE
      p_msg_id number := 1;
      p_cdata xmltype;  
      p_value varchar(50) :='false';
      p_data clob;
    
    BEGIN
      select cdata into p_data from xwarehouses;
      p_cdata := xmltype(p_data);
      select updatexml(p_cdata,'/Warehouse/WaterAccess/text()',p_value) into p_cdata from dual;
      p_data := p_cdata.getclobval();
      update xwarehouses
      set cdata = p_data
      where msd_id = p_msg_id;
      commit;
    
    END;
    
        2
  •  1
  •   Arkadiusz Łukasiewicz    9 年前

    来自ORACLE 11

    不推荐使用所有用于更新XML数据的Oracle SQL函数。神谕 建议改用XQuery Update。这些是 不推荐使用的XML更新函数:

    Migration from Oracle Functions for Updating XML Data to XQuery Update

    UPDATE xwarehouses SET cdata =
      xmlserialize(document XMLQuery('copy $tmp := $source  modify
                (for $i in $tmp/Warehouse/WaterAccess/text()  
                 return replace value of node $i
                        with "false") 
                return $tmp'
               PASSING xmltype(cdata) as "source"  RETURNING CONTENT))
      WHERE cdata IS NOT NULL;