简体   繁体   English

为什么我从这个查询(搜索数据库中的字符串)中没有得到 output?

[英]Why do I get no output from this query (searching database for string)?

I'm an Oracle/PL/SQL Developer newbie, and I'm struggling to figure out how to see the output of this query:我是 Oracle/PL/SQL Developer 新手,我正在努力弄清楚如何查看此查询的 output:

DECLARE
  ncount NUMBER;
  vwhere VARCHAR2(1000) := '';
  vselect VARCHAR2(1000) := ' select count(1) from ';
  vsearchstr VARCHAR2(1000) := '1301 250 Sage Valley Road NW';
  vline VARCHAR2(1000) := '';
  istatus INTEGER;
BEGIN
  DBMS_OUTPUT.ENABLE;
  FOR k IN (SELECT a.table_name, a.column_name FROM user_tab_cols a WHERE a.data_type LIKE '%VARCHAR%')
  LOOP
    vwhere := ' where ' || k.column_name || ' = :vsearchstr ';
    EXECUTE IMMEDIATE vselect || k.table_name || vwhere
      INTO ncount
      USING vsearchstr;
    IF (ncount > 0)
    THEN
      dbms_output.put_line(k.column_name || ' ' || k.table_name);
    ELSE
      dbms_output.put_line('no output');
    END IF;
  END LOOP;
  dbms_output.get_line(vline, istatus);
END;

I got this script from https://community.oracle.com/tech/developers/discussion/2572717/how-to-search-a-particular-string-in-whole-schema .我从https://community.oracle.com/tech/developers/discussion/2572717/how-to-search-a-particular-string-in-whole-schema得到了这个脚本。 It's supposed to find a string ( vsearchstr ) in the entire database.它应该在整个数据库中找到一个字符串( vsearchstr )。 When I run this in PL/SQL Developer 14.0.6, it spits out no errors, says it took 0.172 seconds, but I don't see any output.当我在 PL/SQL Developer 14.0.6 中运行它时,它没有吐出错误,说它花了 0.172 秒,但我没有看到任何 output。 I'm expecting the output to show under the Output tab:我期待 output 显示在 Output 选项卡下:

在此处输入图像描述

I know the string '1301 250 Sage Valley Road NW' exists in the database so it should be finding it.我知道数据库中存在字符串“1301 250 Sage Valley Road NW”,因此应该可以找到它。 Even if it doesn't, the ELSE block should be outputting 'no output'.即使没有,ELSE 块也应该输出“无输出”。

From what I understand, dbms_output.put_line() adds the given string to a buffer, and dbms_output.get_line() prints it to the output target (whatever it's set to).据我了解, dbms_output.put_line() 将给定的字符串添加到缓冲区,而 dbms_output.get_line() 将其打印到 output 目标(无论它设置为什么)。 I understand that dbms_output needs to be enabled (hence the line DBMS_OUTPUT.ENABLE ) and dbms_output.get_line() will only run after the BEGIN/END block it's in completes (I don't know if this means it has to be put outside the BEGIN/END block, but I couldn't avoid certain errors every time I did).我知道需要启用 dbms_output (因此行DBMS_OUTPUT.ENABLE )和 dbms_output.get_line() 只会在它所在的 BEGIN/END 块完成后运行(我不知道这是否意味着它必须放在BEGIN/END 块,但每次我都无法避免某些错误)。

I've read through various stackoverflow posts about this issue, as well as a few external site:我已经阅读了有关此问题的各种 stackoverflow 帖子,以及一些外部站点:

https://docs.oracle.com/cd/F49540_01/DOC/server.815/a68001/dbms_out.htm#1000449 https://www.tutorialspoint.com/plsql/plsql_dbms_output.htm https://docs.oracle.com/cd/F49540_01/DOC/server.815/a68001/dbms_out.htm#1000449 https://www.tutorialspoint.com/plsql/plsql_dbms_output.htm

...but nothing seems to be working. ...但似乎没有任何效果。

How can I see the output, or if there's something wrong in the query above, can you tell what it is?我怎么能看到output,或者如果上面的查询有问题,你能告诉它是什么吗?

Thanks.谢谢。

To enable output from DBMS_OUTPUT in PL/SQL Developer see this answer .要在 PL/SQL Developer 中从DBMS_OUTPUT启用 output,请参阅此答案

I'm looking for an alternative keyword to user_tab_cols for all schemas in the DB我正在为数据库中的所有模式寻找user_tab_cols的替代关键字

Use ALL_TAB_COLS and catch the exceptions when you do not have enough privileges to read the table (and use quoted identifiers to match the case of user/table/column names):当您没有足够的权限来读取表时,使用ALL_TAB_COLS并捕获异常(并使用带引号的标识符来匹配用户/表/列名的大小写):

DECLARE
  found_row  PLS_INTEGER;
  vsearchstr VARCHAR2(1000) := '1301 250 Sage Valley Road NW';
BEGIN
  FOR k IN (SELECT owner,
                   table_name,
                   column_name
            FROM   all_tab_cols t
            WHERE  data_type LIKE '%VARCHAR%'
            -- Ignore columns that are too small
            AND    data_length >= LENGTH(vsearchstr)
            -- Ignore all oracle maintained tables
            -- Not supported on earlier Oracle versions
            AND    NOT EXISTS (
                     SELECT 1
                     FROM   all_users u
                     WHERE  t.owner = u.username
                     AND    u.oracle_maintained = 'Y'
                   )
           )
  LOOP
    DECLARE
      invalid_privileges EXCEPTION;
      PRAGMA EXCEPTION_INIT(invalid_privileges, -1031);
    BEGIN
      EXECUTE IMMEDIATE 'SELECT 1 FROM "' || k.owner || '"."' || k.table_name || '" WHERE "' || k.column_name || '" = :1 AND ROWNUM = 1' 
        INTO  found_row
        USING vsearchstr;

      dbms_output.put_line('Found: ' || k.table_name || '.' || k.column_name);
    EXCEPTION
      WHEN invalid_privileges THEN
        NULL;
      WHEN NO_DATA_FOUND THEN
        dbms_output.put_line('Not found: ' || k.table_name || '.' || k.column_name);
    END;
  END LOOP;
END;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM