这个需求比较冷门,但对于在某些特定的情况下,还是会有这样的需要的。好在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. Java源码阅读Vector

    1类签名与注释 public class Vector<E> extends AbstractList<E> implements List<E>, RandomA ...

  2. 【Hadoop】Hadoop MR 自定义分组 Partition机制

    1.概念 2.Hadoop默认分组机制--所有的Key分到一个组,一个Reduce任务处理 3.代码示例 FlowBean package com.ares.hadoop.mr.flowgroup; ...

  3. 【HTML 元素】嵌入图像

    img元素允许我们在HTML文档里嵌入图像. 要嵌入一张图像需要使用src和alt属性,代码如下: <img src="../img/example/img-map.jpg" ...

  4. Location配置与ReWrite语法

    1 Location语法规则 1.1 Location规则 语法规则: location [=|~|~*|^~] /uri/ {… } 首先匹配 =,其次匹配^~,其次是按文件中顺序的正则匹配,最后是 ...

  5. 北大BBS2008年毕业生晒工资

    http://www.amznz.com/19/快消类: 宝洁:本7200.研8200.博9700,均14个月,另有800交通补助,marketing每9个月 涨20%-30%. 玛氏:月薪10000 ...

  6. Android应用程序开发以及背后的设计思想深度剖析

    1 http://www.uml.org.cn/mobiledev/201211063.asp 2 ...

  7. CSRF介绍与应对以及Java代码示例

    详细信息看这里:https://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/ 简介 CSRF(Cross Site Request Forg ...

  8. Ubuntu 16.04主题美化和软件推荐

    http://www.linuxidc.com/Linux/2016-09/135165.htm http://www.techweb.com.cn/network/system/2015-11-20 ...

  9. (二)spark算子 分为3大类

     transgormation的算子对key-value类型的数据有三种: (1)输入 与 输出为一对一关系 mapValue();针对key-value类型的数据并只对其中的value进行操作,不对 ...

  10. 禁止"Windows Media Player Network Sharing Service"服务自动启动

    开始 -> 运行 -> gpedit.msc -> 计算机配置 -> 管理模板 -> Windows 组件 -> Windows Media Player -> ...