Oracle 11g 中的 RawToHex 函数返回任何原始值的十六进制表示。
此函数将十六进制值作为 varchar2 返回。
如果我将 BLOB 传递到 RawToHex() 函数中会导致超过 varchar2 限制 4000 的十六进制表示会发生什么?
有没有办法将非常大的 BLOB 转换为十六进制表示?
更新:
我做了一些调查,找到了我问题第一部分的答案。
我可以将一个 BLOB 传递到 RawToHex 函数中,只要您不会触及原始数据类型的边界,这个函数就会成功执行。 Oracle 似乎隐式地从 BLOB 转换为 Raw。
DECLARE
a varchar2(32767);
b blob;
BEGIN
select blob_column into b from a_table where a_table_id = 1;
dbms_output.put_line(dbms_lob.getlength(b)); --> output: 216
dbms_output.put_line(rawtohex(empty_blob())); --> converted blob
select blob_column into b from a_table where a_table_id = 2;
dbms_output.put_line(dbms_lob.getlength(b)); --> output: 140000
dbms_output.put_line(rawtohex(empty_blob())); --> ORA-06502: PL/SQL: numeric or value error
END;
根据 ora-code.com 对此错误的描述ORA-06502: PL/SQL: numeric or value error string
Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2).
Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.
更新 2:
我有这个问题的解决方案。将 blob 拆分为更小的块并逐步转换它们。它提供了正确的结果。这是一种正确的方法还是该解决方案在某些时候会失败?
function BlobToHex(data in blob) return clob
is
v_clob clob;
v_start pls_integer := 1;
v_buffer pls_integer := 4000;
begin
if data is null
then
return '""';
end if;
dbms_lob.createtemporary(v_clob, true);
dbms_lob.append(v_clob, '0x');
for i in 1..ceil(dbms_lob.getlength(data) / v_buffer)
loop
dbms_lob.append(v_clob, rawtohex(DBMS_LOB.SUBSTR(data, v_buffer, v_start)));
v_start := v_start + v_buffer;
end loop;
return v_clob;
end;
最佳答案
你会得到 ORA-00932
SQL> select rawtohex(empty_blob()) from dual;
select rawtohex(empty_blob()) from dual
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got BLOB
SQL>
因为
As a SQL built-in function, RAWTOHEX accepts an argument of any scalar data type other than LONG, LONG RAW, CLOB, BLOB, or BFILE.
正如 the documentation 所述.
关于Oracle RawToHex 函数 - 如果返回值超过 varchar2 限制会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8549138/