首页 > 解决方案 > 如何在 oracle 数据库的所有字段/表中查找数据模式匹配项?

问题描述

具体来说,我正在尝试查找已输入到数据库中任何表的文本字段中的任何信用卡号。下面的代码适用于我知道主键是“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;

标签: oracledynamicplsql

解决方案


推荐阅读