2012-07-03 56 views
1

使用Oracle 11g,我有以下LDAP字符串,它只是我在此嘗試演示的一個子集。Oracle字符串文字太長 - 字符串中的數據提取

基本上我有一個非常長的字符串,導致我'字符串文字太長'的問題,基本上在這個字符串,我希望能夠去掉我不想或甚至更好的位,去掉我需要的唯一一點。

這僅僅是字符串內容/長度的短版本:

Member of = 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-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-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=DAaTIGERCall-Admin, CN=aTIGERCall-Admin, 

假設上面的比長4000個字符。我的問題是,使用Oracle SQL和PL/SQL以及上面的「字符串成員」,我需要以某種方式過濾只看起來像'CN = aTIGER%'的位,並完全忽略類似' CN = DAaTIGER%',我相信,我們解決了我的字符串文字問題,但我無法首先過濾出來,因爲我的原始字符串已經超過4000個字符長。

有人可以幫助一種方式使用pl/sql,一種只會返回「CN = aTIGER%」中的「成員」內的條目,並完全忽略類似'CN = DAaTIGER% '同時,確保結果最後還有一個逗號。

我是否需要將它分配給CLOB,然後處理我需要的條目 - 只是不確定如何處理這個問題並解決我的問題?

謝謝。

+1

「CN = D0902498」或「CN = ea90045052」這樣的條目怎麼樣? –

+1

引起錯誤的文字有多長?你有沒有嘗試將字符串分成一個varray? – ipip

回答

2

在下面的代碼中,我預先填充了一個CLOBc」,其中包含一堆數據,例如您的示例。我不確定你想要對每個合格條目做什麼;我只是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> 

我希望這有助於。

+0

你是一個傳奇。謝謝你,真的很感激它。 – tonyf