这个需求比较冷门,但对于在某些特定的情况下,还是会有这样的需要的。好在Oracle实现还比较方便,用存储过程则轻松实现。

查询字符串:

create or replace procedure search_string(pString in varchar) as
cursor all_tab_cursor is
select a.owner, a.table_name, b.column_name
from dba_tables a, dba_tab_columns b, dba_objects c
where a.owner = b.owner
and a.table_name = b.table_name
and a.table_name = c.object_name
--and a.owner in ('XXX') --用户可选
and b.data_type in ('VARCHAR2','CHAR','NCHAR','NCLOB','NVARCHAR2')
and c.object_type = 'TABLE'
order by a.owner,a.table_name,b.column_id;
refAllTab all_tab_cursor%rowtype; -------------- sSql varchar(4000);
nCount number; begin
DBMS_OUTPUT.Enable(4000000); open all_tab_cursor;
loop
fetch all_tab_cursor
into refAllTab;
exit when all_tab_cursor%notfound; sSql := 'SELECT COUNT(1) FROM ' || refAllTab.Owner || '.' ||
refAllTab.Table_Name || ' WHERE ' || refAllTab.Column_Name ||
' = ''' || pString || '''';
--DBMS_OUTPUT.PUT_LINE(sSql);
execute immediate sSql
into nCount; if nCount > 0 then
DBMS_OUTPUT.PUT_LINE(refAllTab.Owner || '.' || refAllTab.Table_Name || '.' ||
refAllTab.Column_Name || ' = ' || nCount);
end if; end loop;
close all_tab_cursor;
end search_string;

查询包含字符串:

create or replace procedure search_string_like(pString in varchar) as
cursor all_tab_cursor is
select a.owner, a.table_name, b.column_name
from dba_tables a, dba_tab_columns b, dba_objects c
where a.owner = b.owner
and a.table_name = b.table_name
and a.table_name = c.object_name
--and a.owner in ('XXX') --用户可选
and b.data_type in ('VARCHAR2','CHAR','CLOB','NCHAR','NCLOB','NVARCHAR2')
and c.object_type = 'TABLE'
order by a.owner,a.table_name,b.column_id;
refAllTab all_tab_cursor%rowtype; -------------- sSql varchar(4000);
nCount number; begin
DBMS_OUTPUT.Enable(4000000); open all_tab_cursor;
loop
fetch all_tab_cursor
into refAllTab;
exit when all_tab_cursor%notfound; sSql := 'SELECT COUNT(1) FROM ' || refAllTab.Owner || '.' ||
refAllTab.Table_Name || ' WHERE ' || refAllTab.Column_Name ||
' LIKE ''%' || pString || '%''';
--DBMS_OUTPUT.PUT_LINE(sSql);
execute immediate sSql
into nCount; if nCount > 0 then
DBMS_OUTPUT.PUT_LINE(refAllTab.Owner || '.' || refAllTab.Table_Name || '.' ||
refAllTab.Column_Name || ' = ' || nCount);
end if; end loop;
close all_tab_cursor;
end search_string_like;

查询数字:

create or replace procedure search_number(pNumber in number) as
cursor all_tab_cursor is
select a.owner, a.table_name, b.column_name
from dba_tables a, dba_tab_columns b, dba_objects c
where a.owner = b.owner
and a.table_name = b.table_name
and a.table_name = c.object_name
--and a.owner in ('XXX') --用户可选
and b.data_type in ('FLOAT','NUMBER')
and c.object_type = 'TABLE'
order by a.owner,a.table_name,b.column_id;
refAllTab all_tab_cursor%rowtype; -------------- sSql varchar(4000);
nCount number; begin
DBMS_OUTPUT.Enable(4000000); open all_tab_cursor;
loop
fetch all_tab_cursor
into refAllTab;
exit when all_tab_cursor%notfound; sSql := 'SELECT COUNT(1) FROM ' || refAllTab.Owner || '.' ||
refAllTab.Table_Name || ' WHERE ' || refAllTab.Column_Name ||
' = ' || pNumber;
--DBMS_OUTPUT.PUT_LINE(sSql);
execute immediate sSql
into nCount; if nCount > 0 then
DBMS_OUTPUT.PUT_LINE(refAllTab.Owner || '.' || refAllTab.Table_Name || '.' ||
refAllTab.Column_Name || ' = ' || nCount);
end if; end loop;
close all_tab_cursor;
end search_number;

查询范围数字:

create or replace procedure search_number_between(pStartNumber in number, pEndNumber in number) as
cursor all_tab_cursor is
select a.owner, a.table_name, b.column_name
from dba_tables a, dba_tab_columns b, dba_objects c
where a.owner = b.owner
and a.table_name = b.table_name
and a.table_name = c.object_name
--and a.owner in ('XXX') --用户可选
and b.data_type in ('FLOAT','NUMBER')
and c.object_type = 'TABLE'
order by a.owner,a.table_name,b.column_id;
refAllTab all_tab_cursor%rowtype; -------------- sSql varchar(4000);
nCount number; begin
DBMS_OUTPUT.Enable(4000000); open all_tab_cursor;
loop
fetch all_tab_cursor
into refAllTab;
exit when all_tab_cursor%notfound; sSql := 'SELECT COUNT(1) FROM ' || refAllTab.Owner || '.' ||
refAllTab.Table_Name || ' WHERE ' || refAllTab.Column_Name ||
' BETWEEN ' || pStartNumber || ' AND ' || pEndNumber;
--DBMS_OUTPUT.PUT_LINE(sSql);
execute immediate sSql
into nCount; if nCount > 0 then
DBMS_OUTPUT.PUT_LINE(refAllTab.Owner || '.' || refAllTab.Table_Name || '.' ||
refAllTab.Column_Name || ' = ' || nCount);
end if; end loop;
close all_tab_cursor;
end search_number_between;

查询日期:

create or replace procedure search_date(pToDateString in varchar) as
cursor all_tab_cursor is
select a.owner, a.table_name, b.column_name
from dba_tables a, dba_tab_columns b, dba_objects c
where a.owner = b.owner
and a.table_name = b.table_name
and a.table_name = c.object_name
--and a.owner in ('XXX') --用户可选
and (b.data_type = 'DATE' or b.data_type like 'TIMESTAMP%')
and c.object_type = 'TABLE'
order by a.owner, a.table_name, b.column_id;
refAllTab all_tab_cursor%rowtype; -------------- sSql varchar(4000);
nCount number; begin
DBMS_OUTPUT.Enable(4000000); open all_tab_cursor;
loop
fetch all_tab_cursor
into refAllTab;
exit when all_tab_cursor%notfound; sSql := 'SELECT COUNT(1) FROM ' || refAllTab.Owner || '.' ||
refAllTab.Table_Name || ' WHERE ' || refAllTab.Column_Name ||
' = ' || pToDateString;
--DBMS_OUTPUT.PUT_LINE(sSql);
execute immediate sSql
into nCount; if nCount > 0 then
DBMS_OUTPUT.PUT_LINE(refAllTab.Owner || '.' || refAllTab.Table_Name || '.' ||
refAllTab.Column_Name || ' = ' || nCount);
end if; end loop;
close all_tab_cursor;
end search_date;

查询范围日期:

create or replace procedure search_date_between(pStartToDateString in varchar, pEndToDateString in varchar) as
cursor all_tab_cursor is
select a.owner, a.table_name, b.column_name
from dba_tables a, dba_tab_columns b, dba_objects c
where a.owner = b.owner
and a.table_name = b.table_name
and a.table_name = c.object_name
--and a.owner in ('XXX') --用户可选
and (b.data_type = 'DATE' or b.data_type like 'TIMESTAMP%')
and c.object_type = 'TABLE'
order by a.owner, a.table_name, b.column_id;
refAllTab all_tab_cursor%rowtype; -------------- sSql varchar(4000);
nCount number; begin
DBMS_OUTPUT.Enable(4000000); open all_tab_cursor;
loop
fetch all_tab_cursor
into refAllTab;
exit when all_tab_cursor%notfound; sSql := 'SELECT COUNT(1) FROM ' || refAllTab.Owner || '.' ||
refAllTab.Table_Name || ' WHERE ' || refAllTab.Column_Name ||
' BETWEEN ' || pStartToDateString || ' AND ' || pEndToDateString;
--DBMS_OUTPUT.PUT_LINE(sSql);
execute immediate sSql
into nCount; if nCount > 0 then
DBMS_OUTPUT.PUT_LINE(refAllTab.Owner || '.' || refAllTab.Table_Name || '.' ||
refAllTab.Column_Name || ' = ' || nCount);
end if; end loop;
close all_tab_cursor;
end search_date_between;

执行范例:

exec search_string('测试');

exec search_string_like('包含测试');

exec search_number(100);

exec search_number_between(100, 200);

exec search_date('to_char(''2013-01-01'',''yyyy-mm-dd'')');

exec search_date_between('to_char(''2013-01-01'',''yyyy-mm-dd'')','to_char(''2014-01-01'',''yyyy-mm-dd'')');

以上存储过程执行完毕后,会输出格式文本:用户.表名.字段名 = 记录数,应用时根据实际情况修改。

转载请注明原文地址:http://www.cnblogs.com/litou/p/3926881.html

[Oracle]根据字段值全库搜索相关数据表和字段的更多相关文章

  1. mysql全库搜索指定字符串

    mysql全库搜索指定字符串 DELIMITER // DROP PROCEDURE IF EXISTS `proc_FindStrInAllDataBase`; # CALL `proc_FindS ...

  2. SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int

    --SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int --关键说明:--1.从系统表syscolumns中的查询所有xtype='48'的记录得到类型为[tinyint]的字段- ...

  3. 批量替换数据库中所有用户数据表中字段数据类型为char和varchar到nvarchar的脚本

    解决问题:字段类型为char的总是占用指定字节长度(末尾好多空白符号),varchar数据类型长度一个汉字占2个字节,内容存储为中文的字段个人建议全部使用nvarchar. 操作说明:打开SQL Se ...

  4. m_Orchestrate learning system---十四、数据表中字段命名规则

    m_Orchestrate learning system---十四.数据表中字段命名规则 一.总结 一句话总结:a.保证唯一 b.见名知意 1.注意php中的数组类函数和字符串类函数的前缀? 数组类 ...

  5. 【SQL Server】sql server更改了数据表的字段/新增数据表的字段 无法保存

    sql server更改了数据表的字段/新增数据表的字段  无法保存 解决方法:进入 工具-->选项-->Designers-->表设计器和数据库设计器-->取消勾选   即可

  6. Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射?

    NET[C#]Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射? 问题描述 比如有如下的数据表结构:Person: person_id int first_name va ...

  7. MySQL 给已存在的数据表 增加字段和注释

    MySQL 给已存在的数据表 增加字段和注释 问题描述 在开发一个系统的过程中,经常会遇到随着系统服务功能的扩展,或者服务之间的关联,需要适当的修改原有的表结构,比如,增加一些必要的字段. 示例:在已 ...

  8. MySQL中大数据表增加字段,增加索引实现

    MySQL中大数据表增加字段,通过增加索引实现 普通的添加字段sql ALTER TABLE `table_name` ADD COLUMN `num` int(10) NOT NULL DEFAUL ...

  9. Oracle 11g快速收集全库统计信息

    环境:Oracle 11.2.0.4 采用并行的方式,快速收集全库统计信息,多用于跨版本升级之后,对全库的统计信息重新进行快速收集: --开启计时 set timing on --设置并行收集 exe ...

随机推荐

  1. fmt标签的格式化日期使用

    声明: <%@ taglib prefix="fmt" uri="/WEB-INF/fmt.tld" %> or <%@ taglib pre ...

  2. 线程阻塞工具类:LockSupport(读书笔记)

     他可以在线程任意位置让线程阻塞, LockSupport的静态方法park()可以阻塞当前线程,类似的还有parkNanos() ParkUntil()等,他们实现了一个限时等待 public cl ...

  3. 从零单排之玩转Python安全编程(II)

    转自:http://www.secpulse.com/archives/35893.html 都说Python大法好,作为一名合格的安全从业人员,不会几门脚本语言都不好意思说自己是从事安全行业的. 而 ...

  4. (CF#257)A. Jzzhu and Children

    There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number al ...

  5. 杭电 HDU 2717 Catch That Cow

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. STL学习笔记(数值算法)

    运用数值算法之前必须先加入头文件<numeric> 加工运算后产生结果 1.对序列进行某种运算 T accumulate(InputIterator beg,InputIterator e ...

  7. MySql 删除相同前缀的表名

    SELECT CONCAT('drop table ', table_name, ';') FROM information_schema.tables WHERE table_name LIKE ' ...

  8. 正则表达式Pattern ,Matcher

    正则表达式:符合一定规则的表达式 作用:用于专门操作字符串 特点:用于一些特定的符号来表示一些代码的操作,这样就简化代码的书写 学习正则表达式就是要学习一些特殊符号的使用 好处:简化对字符串复杂的操作 ...

  9. eclipse debug调试java程序的九个技巧

    九个技巧: 逻辑结构 条件debug 异常断点 单步过滤 跳到帧 Inspect expressions display 远程debug 最早开始用eclipse的debug的时候,只会F5 F6 F ...

  10. springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用

    关于springmvc日期问题的解决方式 除了本博客的[springMVC 前后台日期格式传值解决方式之 @DateTimeFormat的使用和配置]一文, 还有如下这种方式: 在Controller ...