在oracle表中的关键词搜索
在oracle数据库中,我们可以很容易地在模式内的表中找到我们正在寻找的文本或关键词。
请阅读以了解该过程的步骤。
在oracle数据库中,我们可以很容易地在模式内的表中找到我们要找的文本或关键词。由于我们可以搜索模式中的所有表,我们也可以在属于该模式的所需表内进行搜索。
如下面的pl/sql块所示,我们要搜索的文本/关键词是在相关表的varchar列上进行的。
- 首先,我们在pl/sql代码块的declaration部分指定 "p_search_text" –>要搜索的常量文本。
- 在最下面一行,我们指定schema name为一个常量。
- 在下一行中,我们指定了table name。
- 当我们把table name字段传递为空时,我们搜索该模式中的所有表。
- 如果我们把table name传满,我们应该注意该模式中的表。否则,我们的搜索将找不到任何记录。
如上所述输入我们的定义后,我们可以通过oracle sql developer或toad运行下面的pl/sql块。
plsql
declare p_search_text constant varchar2 (1000) := 'sample search write'; p_schema_name constant varchar2 (1000) := 'sample schema name'; p_table_name constant varchar2 (1000) := 'sample table name';-- null (if full will work, give the table name null) type r_column_data is record ( column_name sys.dba_tab_columns.column_name%type ); type tr_column_data is table of r_column_data index by pls_integer; ltr_column_data tr_column_data; type tr_table is table of varchar2 (200) index by pls_integer; l_sql varchar2 (1000); l_count number; ltr_table tr_table; l_found number := 0; l_column_name varchar2 (100); begin select table_name bulk collect into ltr_table from dba_tables where owner = p_schema_name and table_name like '' || p_table_name || '' || '%' and rownum <= 19999 order by 1; dbms_application_info.set_module ('pv_finder', null); for i in 1 .. ltr_table.count loop dbms_application_info.set_client_info ( i || '/' || ltr_table.count || ' -> ' || ltr_table (i)); select col.column_name bulk collect into ltr_column_data from sys.dba_tab_columns col inner join sys.dba_tables t on col.owner = t.owner and col.table_name = t.table_name where col.table_name = ltr_table (i) and col.data_type = 'varchar2' order by col.column_id; for j in 1 .. ltr_column_data.count loop dbms_application_info.set_client_info ( j || '/' || ltr_column_data.count || ' -> ' || ltr_column_data (j).column_name); l_sql := 'select /*+ parallel (a 8)*/ count(1) from ' || p_schema_name || '.' || ltr_table (i) || ' a where ' || ltr_column_data (j).column_name || ' =''' || p_search_text || ''''; execute immediate l_sql into l_count; if l_count > 0 then if l_found = 0 then dbms_output.put_line ( 'search keyword: ' || p_search_text); dbms_output.put_line ( '-------------------------------------------'); end if; dbms_output.put_line ('table name found : '||ltr_table (i) ); dbms_output.put_line ('column name found : ' || ltr_column_data (j).column_name); dbms_output.put_line ('count : ' || l_count); dbms_output.put_line ('sql name : ' || l_sql); dbms_output.put_line ('*****'); l_found := l_found + 1; end if; end loop; end loop; dbms_application_info.set_module (null, null); dbms_application_info.set_client_info (null); dbms_output.put_line ('-------------------------------------------'); dbms_output.put_line ('total number of tables searched : ' || ltr_table.count); dbms_output.put_line ('total number of tables found : ' || l_found); end; /*select module,client_info from v$session where module like '%pv_finder%'*/
结果部分。
- 如果我们搜索的关键词/文本在相关的表中找到了,找到的表的名称和找到的列的名称,数量,以及查询信息都会被写出来。
- 在底部是一般的总信息。我们可以看到搜索到的表的总数以及这些表中有多少被找到。
search keyword: sample search keyword ------------------------------------------- table name found : table - 1 column name found : column name count : 4 sql name : select /*+ parallel (a 8)*/ count(1) from schema_name.table-1 a where table-1.column ='sample search keyword' ***** table name found : table - 2 column name found : column name count : 2 sql name : select /*+ parallel (a 8)*/ count(1) from schema_name.table-2 a where table-1.column ='sample search keyword' ***** table name found : table - 3 column name found : column name count : 2 sql name : select /*+ parallel (a 8)*/ count(1) from schema_name.table-3 a where table-1.column ='sample search keyword' ***** table name found : table - 4 column name found : column name count : 2 sql name : select /*+ parallel (a 8)*/ count(1) from schema_name.table-4 a where table-1.column ='sample search keyword' ***** ------------------------------------------- total number of tables searched : 72 total number of tables found : 4
oracle, plsql, oracle数据库, oracle表, 关键字搜索
dzone贡献者所表达的观点属于他们自己。
到此这篇关于在oracle表中进行关键词搜索的过程的文章就介绍到这了,更多相关oracle关键词搜索内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!
需要了解更多数据库技术:在Oracle表中进行关键词搜索的过程,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/dtteaching/1105485.html