zoukankan      html  css  js  c++  java
  • 根据已知条件,查找该条件出现在那张表中

     1 create or replace procedure p_query_table_by_condition
    2 /**
    3 * name : p_query_table_by_condition
    4 * purpose : 根据条件查该条件出现在那张表
    5 * revisions:
    6 * version date author description
    7 * ---------------- ---------- ------------------ ---------------------------------
    8 * 1.0 2010-12-10 zhangzheng 创建
    9 */
    10
    11 (
    12 v_input in varchar2, --传入要查询的条件
    13 tablename_result out sys_refcursor, --该用户下的所有表
    14 columnname_result out sys_refcursor, --每一个表下的所有列
    15 cur_result out sys_refcursor, --符合条件的记录集
    16 name_text out varchar2 --返回条件所有的表名
    17 ) is
    18 tablename varchar2(100);
    19 columnname varchar2(100);
    20 selectresult varchar2(100);
    21 sql_text varchar2(200);
    22 begin
    23 name_text := '';
    24 sql_text := '';
    25 selectresult := '';
    26 --先查出所有的表的表名存入游标tablename_result中
    27 open tablename_result for
    28 select table_name from user_tables;
    29 --开始循环每一张表
    30 loop
    31 --取出表名放出变量tablename中
    32 fetch tablename_result
    33 into tablename;
    34 exit when tablename_result%notfound;
    35 dbms_output.put_line('table_name: ' || tablename);
    36 --查出每一个表,且类型为varchar2的所有字段名存入游标columnname_result中
    37 open columnname_result for
    38 select column_name
    39 from user_tab_columns
    40 where table_name = tablename
    41 and data_type = 'VARCHAR2';
    42 --开始循环字段记录集游标
    43 loop
    44 --取出字段名存入变量columnname中
    45 fetch columnname_result
    46 into columnname;
    47 exit when columnname_result%notfound;
    48 dbms_output.put_line('columnname: ' || columnname);
    49 --构造查询语句
    50 sql_text := 'select ' || columnname || ' from ' || tablename ||
    51 ' where ' || columnname || ' = ' || '''' ||v_input|| '''';
    52 dbms_output.put_line('sql_text: ' || sql_text);
    53 --为游标cur_result赋值
    54 open cur_result for sql_text;
    55 --循环遍历游标cur_result
    56 loop
    57 fetch cur_result
    58 into selectresult;
    59 exit when cur_result%notfound;
    60 --如果没有,退出本次循环,如果有,则证明输入的条件找到了符合的表名,直接返回
    61 if (cur_result%rowcount = 0) then
    62 exit;
    63 end if;
    64 if (cur_result%rowcount != 0) then
    65 name_text := tablename;
    66 return;
    67 end if;
    68 end loop;
    69 --execute immediate sql_text;
    70 end loop;
    71 end loop;
    72
    73 exception
    74 when others then
    75 rollback;
    76 --打印错误code及错误信息
    77 dbms_output.put_line(sqlcode || '' || sqlerrm);
    78 name_text := '未知';
    79 close tablename_result;
    80 close columnname_result;
    81 end p_query_table_by_condition;
    82 /
  • 相关阅读:
    分片副本监控优化备份分词器03
    es score限制
    German Collegiate Programming Contest 2017 G. Water Testing
    SQL注入以及sqlmap初体验
    BUUCTF [极客大挑战 2019]Upload 1
    DVWA—File Upload 文件上传
    DVWA—XSS 跨站脚本攻击
    WUSTCTF2020 佛说:只能四天
    猪圈密码
    2016全国大学生信息安全竞赛 破译
  • 原文地址:https://www.cnblogs.com/cczz_11/p/2343899.html
Copyright © 2011-2022 走看看