[英]Search In All Tables For A Value (Oracle)
Is it possible to scan through every table in a schema for a particular value in Oracle?是否可以扫描模式中的每个表以查找 Oracle 中的特定值? , small change in the logic, i need to search a specific column (SAMPLE_ABC) in the schema so that i can do research further in which all tables this column SAMPLE_ABC is being used so that i can find the desired result
,逻辑上的小变化,我需要在架构中搜索特定列(SAMPLE_ABC),以便我可以进一步研究在哪些表中使用该列 SAMPLE_ABC 以便我可以找到所需的结果
basically i am searching for a number example column_name - value-12345基本上我正在寻找一个数字示例 column_name - value-12345
i have tried the below but it's running for a very long time....... , i have tried searching for options but i was not able to我已经尝试了以下,但它运行了很长时间.......,我已经尝试搜索选项,但我无法
SET SERVEROUTPUT ON SIZE 100000
DECLARE
match_count INTEGER;
BEGIN
FOR t IN (SELECT owner, table_name, column_name
FROM all_tab_columns
WHERE owner <> 'SYS' and data_type LIKE '%CHAR%') LOOP
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
' WHERE '||t.column_name||' = :1'
INTO match_count
USING '12345';
IF match_count > 0 THEN
dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
END IF;
END LOOP;
END;
/
If you don't want to count appearances of the search string, EXISTS
might be a better option as it'll stop searching through the table as soon as it finds the first appearance.如果您不想计算搜索字符串的出现次数,
EXISTS
可能是一个更好的选择,因为它会在找到第一次出现后立即停止搜索表。 So, I'd suggest something like this;所以,我建议这样的事情; see if it helps.
看看它是否有帮助。
SQL> declare
2 match_count integer;
3 search_string varchar2 (20) := 'MANAGER';
4 l_str varchar2 (1000);
5 begin
6 for t in (select owner, table_name, column_name
7 from all_tab_columns
8 where owner <> 'SYS'
9 and owner in ('SCOTT', 'MIKE')
10 and data_type like '%CHAR%')
11 loop
12 l_str :=
13 'SELECT MAX(1) FROM '
14 || t.owner
15 || '.'
16 || t.table_name
17 || ' a'
18 || ' WHERE EXISTS (SELECT NULL FROM '
19 || t.owner
20 || '.'
21 || t.table_name
22 || ' b'
23 || ' WHERE b.'
24 || t.column_name
25 || ' = a.'
26 || t.column_name
27 || ' AND b.'
28 || t.column_name
29 || ' = :1'
30 || ')';
31
32 execute immediate l_str into match_count using search_string;
33
34 if match_count > 0
35 then
36 dbms_output.put_line (
37 t.table_name || '.' || t.column_name || ' contains "' || search_string || '"');
38 end if;
39 end loop;
40 end;
41 /
EMP.JOB contains "MANAGER"
PL/SQL procedure successfully completed.
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.