拆分CLOB数据类型而不转换为varchar2

时间:2019-09-06 12:32:04

标签: sql oracle plsql

我在CLOB列中保存了多个SQL语句。查询以“;”分隔。我正在尝试读取此CLOB并根据“;”进行拆分。我设法使用现有的在线参考来实现其中的一部分,但是它包括将拆分的CLOB值转换为varchar2数据类型,该数据类型的限制为4K字节。 我想知道是否有其他方法可以实现相似的结果而无需转换数据类型。这意味着对SQL语句没有限制。

with sqltexts as (
select 
  cast(trim(
    regexp_substr(REQUEST_DETAIL, '[^;]+', 1, levels.column_value) 
  ) as varchar2(4000)) as sqltext,REQUEST_ID
from FDP_SQL_SS_SQL_WORKLIST_ALL,
      table(cast(multiset(
        select level from dual 
        connect by level < length (regexp_replace(REQUEST_DETAIL, '[^;]+')) + 1
      ) as sys.OdciNumberList)) levels where request_id=1
)
select distinct sqltext, REQUEST_ID from sqltexts;

CREATE TABLE FDP_SQL_SS_SQL_WORKLIST_ALL (REQUEST_ID NUMBER(30), REQUESTER VARCHAR(50), RACFID VARCHAR2(15), REQUEST_DATE DATE,STATUS VARCHAR2(30),PENDING_WITH VARCHAR2(15), REQUEST_DETAIL CLOB)

INSERT INTO FDP_SQL_SS_SQL_WORKLIST_ALL VALUES( 1,'DUMMY USER2', 'DUSER2', SYSDATE, 'PENDING APPROVAL','CHALKEG', 'CREATE TABLE HUB_SUPPORT.XYZ (COL1 VARCHAR2(10), COL2 NUMBER(10)); SELECT COUNT(*) FROM TAB;');

输出如下

SQLTEXT | REQUEST_ID
CREATE TABLE HUB_SUPPORT.XYZ (COL1 VARCHAR2(10), COL2 NUMBER(10))|  1
SELECT COUNT(*) FROM TAB|   1

1 个答案:

答案 0 :(得分:0)

摘自我对splitting BLOBs的回答:

CREATE OR REPLACE TYPE cloblist IS TABLE OF CLOB;
/

CREATE OR REPLACE FUNCTION split_clob(
  i_str    IN  CLOB,
  i_delim  IN  VARCHAR2 DEFAULT ','
) RETURN cloblist DETERMINISTIC PIPELINED
AS
  p_start        PLS_INTEGER := 1;
  p_end          PLS_INTEGER;
  c_len CONSTANT PLS_INTEGER := DBMS_LOB.GETLENGTH( i_str );
  c_ld  CONSTANT PLS_INTEGER := LENGTH( i_delim );
BEGIN
  IF c_len > 0 THEN
    p_end := DBMS_LOB.INSTR( i_str, i_delim, p_start );
    WHILE p_end > 0 LOOP
      PIPE ROW ( DBMS_LOB.SUBSTR( i_str, p_end - p_start, p_start ) );
      p_start := p_end + c_ld;
      p_end := DBMS_LOB.INSTR( i_str, i_delim, p_start );
    END LOOP;
    IF p_start <= c_len + 1 THEN
      PIPE ROW ( DBMS_LOB.SUBSTR( i_str, c_len - p_start + 1, p_start ) );
    END IF;
  END IF;
END;
/

测试数据

CREATE TABLE test ( data CLOB );

DECLARE
  p_sql CLOB;
BEGIN
  FOR r IN 1 .. 5 LOOP
    p_sql := EMPTY_CLOB();
    FOR s IN 1 .. 3 LOOP
      p_sql := p_sql || 'SELECT '''
                     || DBMS_RANDOM.STRING( 'X', 4000 )
                     || ''' FROM DUAL'
                     || CASE WHEN s < 3 THEN ';' END;
    END LOOP;
    INSERT INTO test ( data ) VALUES ( p_sql );
  END LOOP;
END;
/

查询

SELECT s.*
FROM   test t
       CROSS JOIN
       TABLE( split_clob( t.data, ';' ) ) s

输出

| COLUMN_VALUE                                                                                               |
| :--------------------------------------------------------------------------------------------------------- |
| SELECT 'Q0LXPCB6YO6Z2JXL8WCBNO8SO10APWWCPWYRS1EV40 .... NH364IV18NHNL13X8KCZW5PVLU2ZSBC0A0QKHLW' FROM DUAL |
| SELECT 'DC9WZBGAO5H18FFC8C56XT82IN873N0ALU9Y8LZKQC .... WO5640X3BW8CYLA9B0AHQDPD60GG3XMNUGHAILU' FROM DUAL |
| SELECT 'DGIQMSF92XCA153C3PMN7YJQ5DPNH64NRI14JTZG4D .... 9QP3TBF5FB308E1XC96D1KITRDX3QTN9RJINER7' FROM DUAL |
| SELECT 'ITX107IPUJFRKFU513M6J53F7VLNN59F677D838NAT .... GODY1QSG2SDY9YFRLS39D7YMKKWIMLE4YEFIPKZ' FROM DUAL |
| SELECT 'GWUN4MGOZP6BRSRY18AGNC6K16TGYJE7HNREZOMLAQ .... PB0UL7OZRXX9OV7ETATOL8F1SSC8LGA3YOKHN6B' FROM DUAL |
| SELECT 'VWHF5MSTC1T0NKWWI4D37OZ3QLIRB2JAPR9T718ETM .... N4UA4DM6KLIZRLTACURXGC9PFUWMORKWI53SYDW' FROM DUAL |
| SELECT 'ATWR8EQOGRW12HVB3YQWS5623RMRQZPL6RJ5V3Y66Q .... 3UUY3NYQGEUEN3R135W6MRKVPIHI4HQZRY332GO' FROM DUAL |
| SELECT 'ZIREI4Z7DUZ2WOPYTCBG84OQ8CXHPKF1MXAORQR8KH .... TPJMEO5GYRQUVQ03TI17SCVFZ1JH8UHRJI6FCX7' FROM DUAL |
| SELECT '62J3YDB24934MOD4SQ24MIN8I9W97O8AV2456OXK84 .... ZKP09RUULH52E49NJ0V71ZDPR8V8ZHK5LZ5Y94N' FROM DUAL |
| SELECT 'RYUDIF5AFLDSS3GGRSK999FZ6GA1N9XFIU8QH5O4DM .... 73TH4O9LQK0CK9HDQZ05B503NC62ZMGN0NF3PPZ' FROM DUAL |
| SELECT 'PEYWVEOXJ3WX7NF8VF0J4AP8R8FG9ZG25XW1PTXNR8 .... IXB14MK2I49IY35JGD7WJEHSYOFGX5B916SMMBP' FROM DUAL |
| SELECT 'ADGQIFDMA6H4MCZ5GLQI1RVBKB15LEUTTUYFYS4245 .... E78NIJ4TSFJIOR318S6Q3VYMGY9G3BRZ33C8Q2I' FROM DUAL |
| SELECT 'CBJMJPYQX15AC66GU6H3LUVSVJVQFUMDKBSASUPJH4 .... JBEOLP7GSZ8TTKIEF8448H1LVDBGD04ZLPVJNVS' FROM DUAL |
| SELECT 'DUPA6WXCQCOALD74FPQ4YPBFEO3CHFBS9XEA5FDB71 .... KMG9LE4AD9BZB3XYMQPDDQQP95GKVG9GHU9DGVG' FROM DUAL |
| SELECT 'A3KQ58RAF1HV62DPTRWUHUEQU4LDRYE9QBGJMH04AM .... V3BITNDRS113VSBI0ZP4683UOUJYD5V8GEMF2OC' FROM DUAL |

db <>提琴here