2014-01-30 49 views
4

我試圖尋找一個Oracle數據庫包含某個%20或值%2F任何字符串。我正在查找的值來自未正確編碼的網站,並導致將HTML網址編碼置於值字符串中。搜索Oracle數據庫的字符串轉義

我一直使用下面的腳本來查找數據庫中的數據,卻發現我不能包括對%符號的例外條款。

SET SERVEROUTPUT ON SIZE 100000 

DECLARE 
    match_count INTEGER; 
-- Type the owner of the tables you are looking at 
    v_owner VARCHAR2(255) :='OWNER'; 

-- Type the data type you are look at (in CAPITAL) 
-- VARCHAR2, NUMBER, etc. 
    v_data_type VARCHAR2(255) :='VARCHAR2'; 

-- Type the string you are looking at 
    v_search_string VARCHAR2(4000) :='%\%2F%' ESCAPE '\'; 

--'-- Added to fix syntax highlighting on SO 

BEGIN 
    FOR t IN (SELECT table_name, column_name FROM all_tab_cols where owner=v_owner and data_type = v_data_type AND table_name LIKE '%') LOOP 

    EXECUTE IMMEDIATE 
    'SELECT COUNT(*) FROM '||t.table_name||' WHERE '||t.column_name||' LIKE :1' 
    INTO match_count 
    USING v_search_string; 

    IF match_count > 0 THEN 
     dbms_output.put_line(t.table_name ||' '||t.column_name||' '||match_count); 
    END IF; 

    END LOOP; 
END; 
+0

好像「在大海撈針」式的問題 – Coffee

+1

無關您的問題(我註銷),但你可以改善你的搜索a(bit | lot)...選擇'where rownum = 1'。您只需要在列中找到一個匹配項,並在獲取該列時停止搜索該列。 – Ben

+1

「*卻發現我不能包含%符號*免責條款」 - 確保您可以:http://sqlfiddle.com/#!4/aa71e/2 –

回答

0

,而不是

EXECUTE IMMEDIATE 
'SELECT COUNT(*) FROM '||t.table_name||' WHERE '||t.column_name||' LIKE :1' 
INTO match_count 
USING v_search_string; 

嘗試使用

EXECUTE IMMEDIATE 
'SELECT COUNT(*) FROM '||t.table_name||' WHERE '||t.column_name||' LIKE ''' || v_search_string || '''' 
INTO match_count;