这个需求比较冷门,但对于在某些特定的情况下,还是会有这样的需要的。好在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. curl 执行post请求

    #curl -l -H "Content-type: application/json;charset=UTF-8" -H "X-Forwarded-For: 20.20 ...

  2. How to support both ipv4 and ipv6 address for JAVA code.

    IPv6 have colon character, for example FF:00::EEIf concatenate URL String, IPv6 URL will like: http: ...

  3. etcd的原理分析

    k8s集群使用etcd作为它的数据后端,etcd是一种无状态的分布式数据存储集群. 数据以key-value的形式存储在其中. 今天同事针对etcd集群的运作原理做了一个讲座,总结一下. A. etc ...

  4. 【共享单车】—— React后台管理系统开发手记:员工管理之增删改查

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  5. elasticsearch 基本用法

    最大的特点: 1. 数据库的 database, 就是  index 2. 数据库的 table,  就是 tag 3. 不要使用browser, 使用curl来进行客户端操作.  否则会出现 jav ...

  6. [Algorithms] Using Dynamic Programming to Solve longest common subsequence problem

    Let's say we have two strings: str1 = 'ACDEB' str2 = 'AEBC' We need to find the longest common subse ...

  7. JavaScript 获取文件名,后缀名

    function getBaseName(str) { var segs = str.split('.'); if(segs.length > 1) segs.pop(); return seg ...

  8. 测试用例使用传统excel还是思维导图(Xmind、MindManager等)?

    一.使用感言 实习时随便使用了word文档编写测试用例,也没有人带.后来第一份正式测试工作,也没有人带测试,那时跟着大众学用思维导图写测试用例,发现思维导图非常灵活.目前使用xmind. 使用思维导图 ...

  9. checked exception和unchecked exception区别

    http://blog.csdn.net/yuefengyuan/article/details/6204317 一. Java 中定义了两类异常: 1) Checked exception: 这类异 ...

  10. HTTP协议断点续传

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...