首页 > 解决方案 > 在 oracle 中合并 BLOB 数据

问题描述

我们有一个包含 blob 数据的表(未压缩的,xml 格式的文本)。

我们不能将数据类型更改为 clob 或任何其他类型。

我想合并 2 行 blob 数据以创建新的单行。

因为它是 xml 简单的 concat 不起作用,我需要将它们下载到 unix 然后修改它们然后再次插入同一个表。

blob 没有限制(可以大于 4000 个字符)。

我正在努力寻找将整个 blob 下载到文件中的解决方案。

标签: javaoracleshelloracle11g

解决方案


继 Alex 的评论之后,这里有一个在纯 Oracle SQL 中合并 XML 行(存储为 BLOB)的示例。您没有向我们提供有关您的表结构和数据的很多细节,所以我只是做了一个示例表和数据。如果它们被存储为二进制 XML,我们就必须做一些不同的事情。

-- simple table, just a row id and a blob, and insert 2 rows
create table xml_test (rnum number, x blob);
insert into xml_test values (1, UTL_RAW.CAST_TO_RAW('<ADC><ABC value="1"></ABC></ADC>'));
insert into xml_test values (2, UTL_RAW.CAST_TO_RAW('<ADC><ABC value="2"></ABC></ADC>'));

-- look at the values we just inserted (using my charset id, 873 - for AL32UTF8)
select rnum, xmltype(x, 873)
from xml_test;

-- merge the rows as described and insert as new row with rnum=3
insert into xml_test (rnum, x)
with cs as -- find your charset ID to decode the blob. 873 for me.
    (select NLS_CHARSET_ID(value) as id from nls_database_parameters where parameter='NLS_CHARACTERSET')
SELECT 3 as rn, 
   XMLQuery('copy $i := $row1 modify
                    (for $j in $i/ADC
                     return insert nodes $row2 as last into $j)
                 return $i'
                PASSING xmltype(x1.x, cs.id) as "row1",
                    XMLQuery('ADC/ABC' passing xmltype(x2.x, cs.id) returning content) as "row2"
                RETURNING CONTENT).getBlobVal(cs.id) as x
  FROM xml_test x1
  JOIN xml_test x2 on x2.rnum = 2 -- row 2
  cross join cs
  WHERE x1.rnum = 1; -- row 1

-- look at the new row
select xmltype(x, 873)
from xml_test
where rnum = 3;

-- output:
<?xml version="1.0" encoding="UTF-8"?>
<ADC>
  <ABC value="1"/>
  <ABC value="2"/>
</ADC>

推荐阅读