-2

I was trying to create a PL/SQL program to search for a value in entire database. Following is the code:

DECLARE
  custom_query VARCHAR2(100);
  user_input   VARCHAR2(100);
  i            NUMBER (6);
TYPE temp_record
IS
  varray(1000) OF VARCHAR2(100);
  CURSOR t_rec IS
    SELECT a.name AS t_name,
           b.name AS c_name
    FROM   sys.TABLES a,
           sys.COLUMNS b
    WHERE  a.object_id = b.object_id;

table_rec t_name%ROWTYPE;
BEGIN
  IF NOT t_rec%isopen THEN
    OPEN t_rec;
  END IF;
  FETCH t_rec
  INTO  table_rec;

  WHILE t_rec%FOUND THEN
  LOOP
    custom_query := 'select'
    || table_rec.c_name
    || 'into temp_record from'
    ||table_rec.t_name ;
    dbms_output.Put_line(custom_query);
    EXECUTE IMMEDIATE custom_query;
    FOR i IN 1 .. temp_record.count
    LOOP
      IF (Temp_record(i) = user_input) THEN
        dbms_output.Put_line ('The value you are looking for is in '
        || table_rec.c_name
        || 'column of table'
        || table_rec.t_name);
      END IF;
    END LOOP;
  END LOOP;
END;

Can anyone let me know what's wrong with this code?

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
  • 1
    Could you possibly give us a hint what makes you think that the code is wrong? – PM 77-1 Jan 31 '15 at 23:53
  • What error do you get? Please edit that into your question. – Nathan Tuggy Jan 31 '15 at 23:54
  • 2
    The error messages will tell you what is wrong. The first mistake is that `WHILE t_rec%FOUND THEN LOOP` should not have the `THEN` keyword. After that you're referring to tables that don't exist in Oracle, `sys.tables` and `sys.columns`, and NonPlusUltra has pointed you towards the equivalent Oracle view. Even with this cleaned up it will still be slow and have problems with tables with more than 1000 rows. [There are better ways to do this kind of search](http://stackoverflow.com/a/208892/266304). – Alex Poole Feb 01 '15 at 00:08
  • 1
    No please, don't tell us the errors, we like to guess. – Jeffrey Kemp Feb 01 '15 at 15:19
  • possible duplicate of [Search All Fields In All Tables For A Specific Value (Oracle)](http://stackoverflow.com/questions/208493/search-all-fields-in-all-tables-for-a-specific-value-oracle) – Bob Jarvis - Слава Україні Feb 02 '15 at 03:14

1 Answers1

0

Use ALL_TAB_COLUMNS to iterate all tables and columns in your dynamic query.

Non Plus Ultra
  • 867
  • 7
  • 17