如何在Oracle中将CLOB转换为UTF8 BLOB

时间:2017-11-10 14:24:09

标签: oracle plsql

我需要在Oracle中将数据从CLOB转换为UTF8编码的BLOB。我该怎么办?

1 个答案:

答案 0 :(得分:4)

可以使用以下功能:

CREATE OR REPLACE FUNCTION clob_to_blob(src_clob CLOB) RETURN BLOB IS
    tgt_blob BLOB;
    amount INTEGER := DBMS_LOB.lobmaxsize;
    dest_offset INTEGER := 1;
    src_offset INTEGER  := 1;
    blob_csid INTEGER := nls_charset_id('UTF8');
    lang_context INTEGER := DBMS_LOB.default_lang_ctx;
    warning INTEGER := 0;
begin
    if src_clob is null then
        return null;
    end if;

    DBMS_LOB.CreateTemporary(tgt_blob, true);
    DBMS_LOB.ConvertToBlob(tgt_blob, src_clob, amount, dest_offset, src_offset, blob_csid, lang_context, warning);
    return tgt_blob;
end clob_to_blob;

用法

UPDATE mytable SET column1_blob = clob_to_blob(column2_clob);