Oracle-利用自定义函数将Blob转为string(主要用于图片传输)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
--创建一个将BLOB转换为string的函数:
CREATE OR REPLACE FUNCTION base64encode(p_blob IN BLOB) RETURN CLOB IS
l_clob CLOB;
l_step PLS_INTEGER := 12000;
BEGIN
FOR i IN 0 .. TRUNC((DBMS_LOB.getlength(p_blob) - 1) / l_step) LOOP
l_clob := l_clob ||
UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(DBMS_LOB.substr(p_blob,
l_step,
i *
l_step + 1)));
END LOOP;
RETURN l_clob;
END;

--调用
SELECT base64encode(field) FROM DUAL