我一直担心 Oracle 如何处理 CLOB 数据,所以这里有一个测试表明 Pivot 解决方案应该可以解决问题。
drop table emptest;
-- Assuming we are using the venerable EMP table
create table emptest as select * from emp;
alter table emptest add(
text1 CLOB,
text2 CLOB,
text3 CLOB
)
/
declare
v_text clob;
begin
-- set one column to a length well beyond 16k but below 32k, max VARCHAR2 for PL/SQL
v_text := lpad('X', 16000, 'X')||' unemployed ' || lpad('X', 10000, 'X');
update emptest set text2 = v_text where ename = 'SMITH';
-- set others to short values
v_text := 'an unemployed salesman in text 1';
update emptest set text1 = v_text where ename = 'TURNER';
v_text := 'an unemployed manager in text 3';
update emptest set text3 = v_text where ename = 'JONES';
commit;
end;
/
declare
v_clob clob;
begin
-- Set a field to an absurdly long value, with the match value way beyond 32k.
update emptest set text1 = empty_clob() where ename = 'SMITH' returning text1 into v_clob;
for i in 1..10000 loop
dbms_lob.writeappend(v_clob, 36, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
end loop;
dbms_lob.writeappend(v_clob, 18, 'unemployed manager');
commit;
end;
/
select empno, ename, clob_name, clob_value, length(clob_value) clob_length
from emptest unpivot (clob_value for clob_name in (text1, text2, text3))
where clob_value like '%unemployed%'
/
这样做的结果将是:
EMPNO ENAME CLOB_NAME CLOB_VALUE CLOB_LENGTH
----- ------- --------- ----------- -----------
7566 JONES TEXT3 <excluded> 31
7369 SMITH TEXT1 <excluded> 360018
7369 SMITH TEXT2 <excluded> 26012
7844 TURNER TEXT1 <excluded> 32
在处理TEXT1 的SMITH 时,Oracle 如何处理LIKE 关键字非常重要:请注意,该列的长度大于360k 个字符。
我们尝试与CLOB 数据类型一起使用的大部分标准语法之所以有效,是因为Oracle 将CLOB 强制转换为VARCHAR2,但这具有固有的长度限制。
正如该测试所示,LIKE 比较确实适用于胖 CLOB 值——至少在我测试过的 Oracle 12c 中。
如果您尝试显示匹配的实际内容,情况会有所不同:您需要熟悉 DBMS_LOB 包及其子程序,例如 DBMS_LOB.INSTR 和 DBMS_LOB.SUBSTR 如果您正在处理 long @ 987654335@ 值。