这是一种选择;看看它是否有帮助。读取代码中的 cmets。
由于我在本示例中使用 SQL*Plus,因此我启用了输出:
SQL> set serveroutput on;
示例一:
SQL> -- Looking for a string: SCOTT
SQL>
SQL> declare
2 l_str varchar2(500); -- SELECT statement to be used as dynamic SQL
3 l_value varchar2(20); -- value you are looking for
4 l_cnt number; -- number of rows that contain L_VALUE value
5 begin
6 -- I'm looking for a string SCOTT in current schema
7 l_value := 'SCOTT';
8
9 -- Loop through all tables; I'm restricting the list to only two tables.
10 -- You'd probably want to remove the WHERE clause
11 for cur_t in (select table_name
12 from user_tables
13 where table_name in ('EMP', 'DEPT')
14 ) loop
15 -- Loop through all columns in every table from CUR_T
16 for cur_c in (select column_name
17 from user_tab_columns
18 where table_name = cur_t.table_name
19 -- Adjust data type, according to L_VALUE value because if
20 -- you're looking for a string, you can't just compare it to
21 -- e.g. DATE datatype; you'll get the "invalid number" error
22 and data_type like '%CHAR%'
23 ) loop
24 -- Compose a SELECT statement. It might differ, depending on value you're
25 -- looking for. Strings have to be enclosed into single quotes (CHR(39))
26 l_str := 'select count(*) from ' || cur_t.table_name ||
27 ' where '|| cur_c.column_name || ' = ' ||
28 chr(39) || l_value || chr(39);
29
30 -- To make sure it is correctly written, display it first using
31 -- dbms_output.put_line(l_str);
32
33 -- Run the SELECT statement
34 execute immediate l_str into l_cnt;
35
36 if l_cnt > 0 then
37 -- SCOTT was found in some table; display it
38 dbms_output.put_line(cur_t.table_name ||'.'|| cur_c.column_name ||
39 ' contains ' || l_cnt || ' values I am looking for');
40 end if;
41 end loop;
42 end loop;
43 end;
44 /
EMP.ENAME contains 1 values I am looking for
PL/SQL procedure successfully completed.
SQL>
示例二:我修改的行是#3、7、22和28(搜索数字而不是字符串):
SQL> -- Looking for number "10"
SQL>
SQL> declare
2 l_str varchar2(500); -- SELECT statement to be used as dynamic SQL
3 l_value number; -- value you are looking for
4 l_cnt number; -- number of rows that contain L_VALUE value
5 begin
6 -- I'm looking for a number 10 in current schema
7 l_value := 10;
8
9 -- Loop through all tables; I'm restricting the list to only two tables.
10 -- You'd probably want to remove the WHERE clause
11 for cur_t in (select table_name
12 from user_tables
13 where table_name in ('EMP', 'DEPT')
14 ) loop
15 -- Loop through all columns in every table from CUR_T
16 for cur_c in (select column_name
17 from user_tab_columns
18 where table_name = cur_t.table_name
19 -- Adjust data type, according to L_VALUE value because if
20 -- you're looking for a string, you can't just compare it to
21 -- e.g. DATE datatype; you'll get the "invalid number" error
22 and data_type = 'NUMBER'
23 ) loop
24 -- Compose a SELECT statement. It might differ, depending on value you're
25 -- looking for. Strings have to be enclosed into single quotes (CHR(39))
26 l_str := 'select count(*) from ' || cur_t.table_name ||
27 ' where '|| cur_c.column_name || ' = ' ||
28 l_value;
29
30 -- To make sure it is correctly written, display it first using
31 -- dbms_output.put_line(l_str);
32
33 -- Run the SELECT statement
34 execute immediate l_str into l_cnt;
35
36 if l_cnt > 0 then
37 -- "10" was found in some table; display it
38 dbms_output.put_line(cur_t.table_name ||'.'|| cur_c.column_name ||
39 ' contains ' || l_cnt || ' values I am looking for');
40 end if;
41 end loop;
42 end loop;
43 end;
44 /
DEPT.DEPTNO contains 1 values I am looking for
EMP.DEPTNO contains 3 values I am looking for
PL/SQL procedure successfully completed.
SQL>