【问题标题】:Oracle string literal too long - extraction of data within a stringOracle 字符串文字太长 - 提取字符串中的数据
【发布时间】:2012-07-03 10:44:18
【问题描述】:

使用 Oracle 11g,我有以下 LDAP 字符串,这只是我在此处尝试演示的内容的一个子集。

基本上我有一个很长的字符串导致我出现“字符串文字太长”的问题,基本上在这个字符串中,我希望能够去掉我不想要的位,甚至更好,去掉只有我需要的部分。

这只是字符串内容/长度的简短版本:

成员 = CN=aTIGERAdmin-Admin, CN=D0902498, CN=ea90045052, CN=aTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=aTIGERAdmin-Admin, CN=ea90045052, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-管理员,CN=DAaTIGERCall-Admin,CN=DAaTIGERCall-Admin,CN=DAaTIGERCall-Admin,CN=DAaTIGERCall-Admin,CN=DAaTIGERCall-Admin,CN=DAaTIGERCall-Admin,CN=aTIGERCall-Admin,CN=aTIGERAdmin-Admin, CN=D0902498, CN=ea90045052, CN=aTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=aTIGERAdmin-Admin, CN=ea90045052, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-管理员,CN=DAaTIGERCall-Admin,CN=DAaTIGERCall-Admin,CN=DAaTIGERC全管理员,CN=DAaTIGERCall-Admin,CN=aTIGERCall-Admin,

假设上面的长度大于 4000 个字符。

我的问题是,使用 Oracle SQL 和 PL/SQL 以及上面的“成员”字符串,我需要以某种方式只过滤掉看起来像 'CN=aTIGER%' 的位,并完全忽略看起来像 'CN 的条目=DAaTIGER%' 我相信,我们解决了我的字符串文字问题,但我无法先过滤掉它,因为我的原始字符串已经超过 4000 个字符长。

使用 pl/sql,我正在寻找一种方法,它只返回“成员”中看起来像 'CN=aTIGER%' 的条目,同时完全忽略看起来像 'CN=DAaTIGER%' 的条目, 确保结果末尾还有一个逗号。

我是否需要将其分配给 CLOB,然后处理我需要的条目?

【问题讨论】:

  • CN=D0902498CN=ea90045052 等条目怎么样?
  • 导致错误的文字多长时间?您是否尝试将字符串拆分为可变数组?

标签: sql regex oracle plsql oracle11g


【解决方案1】:

在下面的代码中,我预先填充了一个 CLOB "c" ,其中包含一堆数据,例如您的示例。我不确定您想对每个符合条件的条目做什么;我只是DBMS_OUTPUT它。

考虑到这一切,这里是一个尝试:

SET SERVEROUTPUT ON
DECLARE
    c                   CLOB;
    l_offset            POSITIVE := 1;
    l_ldap_component    LONG;
    l_counter           NATURAL := 0;
BEGIN
    -- Set up the CLOB.  In real life, this data will come
    -- from some other source.
    c := 'Member of = CN=aTIGERAdmin-Admin'
      || ', CN=D0902498'
      || ', CN=ea90045052'
      || ', CN=aTIGERCall-Admin'
      || ', CN=DAaTIGERCall-Admin'
--- Lots of additional data excised for the sake of brevity....
      || ', CN=aTIGERCall-Admin,';
    DBMS_OUTPUT.PUT_LINE('Length of CLOB = ' || TO_CHAR(DBMS_LOB.GETLENGTH(c)));

    -- Search for commas within the CLOB, and extract each subsequent
    -- inter-comma string, including the trailing comma.
    WHILE (DBMS_LOB.INSTR(c,',',l_offset) > 0)
    LOOP
        l_ldap_component :=
            TRIM (
                DBMS_LOB.SUBSTR(c
                ,               DBMS_LOB.INSTR(c
                                ,              ','
                                ,              l_offset) - l_offset + 1
                ,               l_offset)
        );
        IF (l_ldap_component LIKE 'CN=aTIGER%,') THEN
            -- I'm printing the qualified entries to the screen, but you
            -- can add them to a collection or whatever....
            DBMS_OUTPUT.PUT_LINE(l_ldap_component);
        END IF;
        l_offset := DBMS_LOB.INSTR(c,',',l_offset) + 1;
    END LOOP;    
END;
/

使用我使用的数据,这段代码产生了输出:

Length of CLOB = 5127
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,
CN=aTIGERAdmin-Admin,
CN=aTIGERCall-Admin,

PL/SQL procedure successfully completed.

SQL>

我希望这会有所帮助。

【讨论】:

  • 你是个传奇。谢谢你,真的很感激。
猜你喜欢
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
相关资源
最近更新 更多