-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?

5
  • 1
    Could you possibly give us a hint what makes you think that the code is wrong?
    – PM 77-1
    Commented Jan 31, 2015 at 23:53
  • What error do you get? Please edit that into your question. Commented Jan 31, 2015 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.
    – Alex Poole
    Commented Feb 1, 2015 at 0:08
  • 1
    No please, don't tell us the errors, we like to guess. Commented Feb 1, 2015 at 15:19

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.