oracle遍历所有表所有列中的内容
我这里只写了:知道它对应的字段的值,如果不知道对应的字段,那么会出问题。因为存在类型转换的问题。以下供您参考。 DECLARE tab_name VARCHAR(10); --表名 col_name VARCHAR(10); --字段名字 col_values VARCHAR(10); --字段值 sqlStr VARCHAR(500); --拼写sql flag int; --判断是否存在 recCount int; -- 记录数 cursor cur is --定义游标 select * from user_col_comments where column_name = 'AAB034'; BEGIN recCount := 0; col_values := '654000'; --字段值 dbms_output.put_line('表名 ' || chr(32) || '字段 ' || chr(32) || '字段值'); for i in cur loop tab_name := i.table_name; col_name := i.column_name; sqlStr := 'select count(*) from ' || tab_name || ' where AAB034='|| chr(39) || col_values || chr(39); --chr(39) 为单引号 execute immediate sqlStr into flag; if flag > 0 then dbms_output.put_line('-----------------------'); dbms_output.put_line(tab_name || chr(32) || col_name || chr(32) || col_values); recCount := recCount + 1; end if; end loop; dbms_output.put_line(chr(10)||'共有 ' || recCount || ' 条记录'); end;答案补充
但是你总要知道数据的类型,如果不知道数据类型,只能手动修改了 set serveroutput on size 1000000 ; 游标改为: cursor cur is select * from USER_TAB_COLS where data_type ='VARCHAR2'; --数据的类型 拼写sql 改为: sqlStr := 'select count(*) from ' || tab_name || ' where '||col_name||'='||chr(39) || col_values || chr(39); if 改为: while flag > 0 loop dbms_output.put_line('-----------------------'); dbms_output.put_line(tab_name || chr(32) || col_name || chr(32) ||col_values); flag := flag - 1; recCount := recCount + 1; end loop;
不知道你是写程序用,还是找数据用。写程序的话,一步到位,可能比较复杂! 下面附一点找数据的较笨的方法!看对你有没有用,以用户SCOTT为例: 1.dba_tab_columns有某个用户的所以表和列等信息:select table_name,column_name from dba_tab_columns where owner='SCOTT' 2.查看用户的表所使用的数据类型有哪些:select distinct Data_type from (select * from dba_tab_columns where owner='SCOTT') A 通过查看,就可以知道,文本型有哪些?如:是否有char, varchar2等 3.创建一个表,用于保留那些表名和字段:CREATE TABLE SCOTT.TCOL(A VARCHAR2(50), B VARCHAR2(50)) 4.因为SCOTT用户的字段类型较少,下面以找NUMBER类型值为1100的表名与字段在SQLPLUS下运行如下代码(如果数值多的话,可以用 in('NUMBER',...)格式。 set serveroutput on delete from scott.TCOL; commit; declare cursor my_cursor is select table_name,column_name from dba_tab_columns where owner='SCOTT' and DATA_TYPE='NUMBER'; v_table varchar2(50); v_col varchar2(50); v_sql varchar2(4000); begin open my_cursor; loop fetch my_cursor into v_table,v_col; dbms_output.put_line(v_table); exit when my_cursor%NOTFOUND; v_sql:='Insert into SCOTT.TCOL(A,B) select '||''''|| v_table||''''||','||''''|| v_col||''''||' from SCOTT.'||v_table||' where '||v_col||'=1100'; dbms_output.put_line(v_sql); EXECUTE IMMEDIATE v_sql; commit; end loop; close my_cursor; end; 5.最后执行select * from scott.tcol,就可以看到结果! 如果有用,自己再整理成一个存储过程.
declare
m_str varchar2(300);
m_int int;
begin
for x in (select *
from col
where tname in (select tname from tab where tabtype = 'TABLE' and tname not like 'BIN%')
and coltype in ('VARCHAR2', 'CHAR')) loop
m_str := 'select count(*) from ' || x.tname || ' where ' || x.cname ||' like ''zhaogh%'' ';
execute immediate m_str into m_int;
-- dbms_output.put_line(m_str);
if m_int > 0 then
dbms_output.put_line(x.tname || '.' || x.cname);
end if;
end loop;
end;