Oracle通过CLOB循环获取字符串

时间:2017-05-15 22:21:52

标签: oracle substr clob

我想将CLOB中的所有数据转换为字符串。 DBMC_LOB.SUBSTR提供了一次获取4000个字符的方法。我来自MS SQL后台,并且不了解Oracle查询。有人可以帮我解释语法。

我想创建一个函数并执行类似

的操作
// Get the length of CLOB
// totalIterationsRequired = length/4000;
// LOOP around the CLOB and fetch 4000 char at once 
For i in 1..totalIterationsRequired
LOOP
// Insert the substring into a varchar2
//  DBMC_LOB.SUBSTR(xml_value,4000*i,(4000*(i-1)+1)
END LOOP
// The function will return the varchar2

2 个答案:

答案 0 :(得分:2)

create table save_table(rec varchar2(4000));

create or replace PROCEDURE save_clob (p_clob IN CLOB)
AS
    l_length             INTEGER;
    l_start              INTEGER := 1;
    l_recsize   CONSTANT INTEGER := 4000;
BEGIN
    l_length   := DBMS_LOB.getlength (p_clob);

    WHILE l_start <= l_length
    LOOP
        INSERT INTO save_table (rec)
             VALUES (DBMS_LOB.SUBSTR (p_clob, l_recsize, l_start));

        l_start   := l_start + l_recsize;
    END LOOP;
END save_clob;

答案 1 :(得分:0)

以下是功能定义

create or replace function getclobastable (id IN VARCHAR2) 
  return clob_table
is
    l_length             INTEGER;
    l_start              INTEGER := 1;
    l_recsize   CONSTANT INTEGER := 4000;
    i_clob                 CLOB;
    n BINARY_INTEGER := 0;
    save_table clob_table := clob_table();
BEGIN

    save_table := clob_table();
    -- Get the CLOB data into i_clob
    l_length := DBMS_LOB.getlength (i_clob);

    WHILE l_start <= l_length
    LOOP
        n := n + 1;

        save_table.EXTEND();
        save_table(n) := DBMS_LOB.SUBSTR (i_clob, l_recsize, l_start);

        l_start   := l_start + l_recsize;
    END LOOP;
    RETURN save_table;
END;