如何在Oracle数据库的所有字段/表中找到数据模式匹配?

时间:2018-07-24 21:30:31

标签: oracle dynamic plsql

具体地说,我正在尝试查找已在数据库中任何表的文本字段中键入的任何信用卡号。下面的代码适用于单个表,在该表中我知道主键是“ ID”,但是我找不到一种使之动态的方法,以便可以遍历数百个表和行。

drop table tab1 purge;

create table tab1(id number primary key, col1 varchar2(20), col2 varchar2(20), col3 varchar2(20));

Insert into TAB1 Values (1, 'No card here', 'Hello', 'nothing');

Insert into TAB1 Values (2, '1111222233334444', 'Visa', 'Hello');

Insert into TAB1 Values (3, 'Hello', 'MasterCard', '1111 2222 3333 4444');

Insert into TAB1 Values (4, 'Hello', '1111-2222-3333-4444', 'Visa');

Insert into TAB1 Values (5, 'Amex', 'Hello', '1111 222222 33333');

Insert into TAB1 Values (6, '111122222233333', 'Amex', 'Hello');

Insert into TAB1 Values (7, 'nothing', 'No card here', 'Hello');

COMMIT;


declare

    myrow clob;

begin

    for i in (select id from tab1) loop
        select dbms_xmlgen.getxml('select * from tab1 where id=' || i.id) into myrow from dual;
        if regexp_instr(myrow,'[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}[ -]?[0-9]{4}|[0-9]{4}[ -]?[0-9]{6}[ -]?[0-9]{5}') > 0 then
            dbms_output.put_line('May have found one: ' || to_char(i.id));
        end if;
    end loop;
end;

0 个答案:

没有答案