Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: November 16, 2016 - 8:55 pm UTC

Last updated: November 18, 2016 - 4:16 am UTC

Version: Oracle 11g

Viewed 1000+ times

You Asked

Hi , I need to write a procedure which has a cursor that stores the list of table names. We need to loop the cursor and for each table name it should look for a code in that particular table. The final output of the procedure should be the list of table names which contains the required codes. Can someone help me in writing this procedure.

Thanks

and Connor said...


SQL> drop table t purge;

Table dropped.

SQL> create table t ( tname varchar2(30), colname varchar2(30));

Table created.

SQL>
SQL> insert into t values ('DEPT','DNAME');

1 row created.

SQL> insert into t values ('EMP','ENAME');

1 row created.

SQL>
SQL> set serverout on
SQL> declare
  2    c int;
  3    string_to_look_for varchar2(10) := 'AL';
  4  begin
  5   for i in ( select * from t ) loop
  6     execute immediate
  7       'select count(*) from scott.'||i.tname||' where '||i.colname||' like ''%'||string_to_look_for||'%'''
  8       into c;
  9
 10     dbms_output.put_line('Found '||c||' occurrences in '||i.tname);
 11   end loop;
 12  end;
 13  /
Found 1 occurrences in DEPT
Found 1 occurrences in EMP

PL/SQL procedure successfully completed.

SQL>
SQL> set serverout on
SQL> declare
  2    c int;
  3    string_to_look_for varchar2(10) := 'A';
  4  begin
  5   for i in ( select * from t ) loop
  6     execute immediate
  7       'select count(*) from scott.'||i.tname||' where '||i.colname||' like ''%'||string_to_look_for||'%'''
  8       into c;
  9
 10     dbms_output.put_line('Found '||c||' occurrences in '||i.tname);
 11   end loop;
 12  end;
 13  /
Found 4 occurrences in DEPT
Found 7 occurrences in EMP

PL/SQL procedure successfully completed.

SQL>
SQL>



If "string_to_look_for" is going to be provided by end users, then you MUST add logic to guard against sql injection.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Error message

sirisha, November 17, 2016 - 7:02 pm UTC

HI, I tried this today with little modifications, but I am getting the following error messages.
sql> set serverout on
sql> declare
2 c int;
3 begin
4 for i in (select * from table) loop
5 execute immediate
6 'select count(*) from'||i.owner||'.'||i.table_name||'where code not in (''xyz'')'
7 into c;
8 dbms_output.put_line('found'||c||'occurrences in'||i.owner||'.'||i.table_name);
9 end loop;
10 end;
11 /
declare
*
error at line 1:
ora 00933:sql command not properly ended
ora 06512:at line 5

SQL>

next time when i ran the same code the following error
ora 00923: from keyword not found where expected
ora 06512: at line5
Connor McDonald
November 18, 2016 - 4:16 am UTC

show us your *whole* script from top to bottom

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library