Solved

Oracle SQL Syntax

Posted on 2016-09-19
8
59 Views
Last Modified: 2016-09-21
Hello Experts,

Is there a way to search an Oracle 12c database to find a specific value in a specific column for all tables that have that column? For example, if the column name is CASE_ID and the value that I am looking for is the string 'abcdefghijk' , I want to know all tables that have the CASE_ID column populated with that value.
0
Comment
Question by:jsmith08
  • 4
  • 2
  • 2
8 Comments
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 41804936
As a one time exercise, I would do it this way:
SELECT 'select ''' 
       || owner 
       || ''' owner, ''' 
       || table_name 
       || ''' table_name, count(1) match_count from ' 
       || owner 
       || '.' 
       || table_name 
       || ' where case_id = ''abcdefghijk'';' 
FROM   dba_tab_columns 
WHERE  column_name = 'CASE_ID'; 

Open in new window

This will give you a list of SQL commands to run.  Spool the output from this query into a file and that will give you a script to run.  Then just run that script file and it will give you a count for every table that contains a column named CASE_ID.
0
 
LVL 31

Expert Comment

by:awking00
ID: 41804962
Using PL/SQL with dynamic sql can also get you what you need.
declare
v_sql varchar2(255);
v_search varchar2(255) := 'abcdefghijk';
v_cnt pls_integer;
begin
for t in
(select table_name from user_tables
 where column_name = 'CASE_ID'
 order by table_name)
loop
v_sql = 'select count(*) from '||t.table_name||' where case_id = '||v_search;
execute immediate v_sql into v_cnt;
if v_cnt > 0 then
 dbms_output.put_line(t.table_name||' is populated with '||v_search;
end if;
end loop;
end;
/
You may want to change user_tables to all_tables or dba_tables depending on your need and privileges.
0
 
LVL 34

Expert Comment

by:johnsone
ID: 41804988
You don't need nested loops in PL/SQL to do it.  Use the same query generating SQL that you would use to spool the statements, then just loop through the results.
DECLARE 
    v_cnt   PLS_INTEGER; 
    v_owner VARCHAR2(30); 
    v_table VARCHAR2(30); 
BEGIN 
    FOR c1rec IN (SELECT 'select ''' 
                         || owner 
                         || ''' owner, ''' 
                         || table_name 
                         || ''' table_name, count(1) match_count from ' 
                         || owner 
                         || '.' 
                         || table_name 
                         || ' where case_id = ''abcdefghijk''' stmt 
                  FROM   dba_tab_columns 
                  WHERE  column_name = 'CASE_ID') LOOP 
        EXECUTE IMMEDIATE c1rec.stmt INTO v_owner, 
                                          v_table, 
                                          v_cnt; 

        dbms_output.Put_line(v_owner 
                             || '.' 
                             || v_table 
                             || ' = ' 
                             || v_cnt); 
    END LOOP; 
END; 

/ 

Open in new window

This is the beauty of using SQL to generate SQL statements.  You don't need to keep iterating through loops to get to the statement.  You need dynamic SQL anyway, just run one query to generate the full statement for you.

I don't care for doing it this way.  Dynamic SQL isn't very efficient and DBMS_OUTPUT can be troublesome if there are a lot of tables involved (you can blow out the internal buffer pretty easily, even at maximum).  Scripts and spooling get around this.
0
 

Author Comment

by:jsmith08
ID: 41805148
When I try the 1st suggestion:
SELECT 'select '''
       || owner
       || ''' owner, '''
       || table_name
       || ''' table_name, count(1) match_count from '
       || owner
       || '.'
       || table_name
       || ' where case_id = ''abcdefghijk'';'
FROM   all_tab_columns
WHERE  column_name = 'CASE_ID';

it appears to be returning a row for every table that has that column name regardless of
whether or not my value really exists in that table because when I do a simple select *
from... on several of the tables, it doesn't return any rows.

When I try the 2nd suggestion:
declare
v_sql varchar2(255);
v_search varchar2(255) := 'abcdefghijk';
v_cnt pls_integer;
begin
for t in
(select table_name from all_tab_columns
 where column_name = 'CASE_ID'
 order by table_name)
loop
v_sql = 'select count(*) from '||t.table_name||' where case_id = '||v_search;
execute immediate v_sql into v_cnt;
if v_cnt > 0 then
 dbms_output.put_line(t.table_name||' is populated with '||v_search;
end if;
end loop;
end;
/

I get the following error report:

Error report:
ORA-06550: line 11, column 7:
PLS-00103: Encountered the symbol "=" when expecting one of the following:

   := . ( @ % ;
ORA-06550: line 12, column 1:
PLS-00103: Encountered the symbol "EXECUTE"
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

When I try the 3rd suggestion:

DECLARE
    v_cnt   PLS_INTEGER;
    v_owner VARCHAR2(30);
    v_table VARCHAR2(30);
BEGIN
    FOR c1rec IN (SELECT 'select '''
                         || owner
                         || ''' owner, '''
                         || table_name
                         || ''' table_name, count(1) match_count from '
                         || owner
                         || '.'
                         || table_name
                         || ' where case_id = ''abcdefghijk''' stmt
                  FROM   all_tab_columns
                  WHERE  column_name = 'CASE_ID') LOOP
        EXECUTE IMMEDIATE c1rec.stmt INTO v_owner,
                                          v_table,
                                          v_cnt;

        dbms_output.Put_line(v_owner
                             || '.'
                             || v_table
                             || ' = '
                             || v_cnt);
    END LOOP;
END;

/

I  get the following error report:

Error report:
ORA-00933: SQL command not properly ended
ORA-06512: at line 17
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 34

Expert Comment

by:johnsone
ID: 41805156
The first one should return a row for every table that has a column CASE_ID in it.  There is a count on each one.  You can always filter out the counts of 0 from the output.

This is the second one with the syntax errors corrected:
DECLARE 
    v_sql    VARCHAR2(255); 
    v_search VARCHAR2(255) := 'abcdefghijk'; 
    v_cnt    PLS_INTEGER; 
BEGIN 
    FOR t IN (SELECT table_name 
              FROM   all_tab_columns 
              WHERE  column_name = 'CASE_ID' 
              ORDER  BY table_name) LOOP 
        v_sql := 'select count(*) from ' 
                 ||t.table_name 
                 ||' where case_id = ' 
                 ||v_search; 

        EXECUTE IMMEDIATE v_sql INTO v_cnt; 

        IF v_cnt > 0 THEN 
          dbms_output.Put_line(t.table_name 
                               ||' is populated with ' 
                               ||v_search); 
        END IF; 
    END LOOP; 
END; 

/ 

Open in new window

However, since ALL_TAB_COLUMNS is used, no owner is selected or added to the dynamic query, so it will fail if the user you are running the block as does not own the table.

The third one works perfectly fine on my system.  No errors.
0
 
LVL 31

Expert Comment

by:awking00
ID: 41805377
Sorry for the confusion. v_sql = 'select ...' was meant to be v_sql := 'select ...'
johnsone, thanks for the correction.
0
 
LVL 34

Expert Comment

by:johnsone
ID: 41805400
There was also a missing ) on the DBMS_OUTPUT function call.

No problem.  I already had a window open so just threw it in to get the syntax fixed.

I'm not sure why people that ask questions aren't capable of figuring that out on their own.  Most of the time we just free type these in.
0
 

Author Comment

by:jsmith08
ID: 41808841
Thank you both for your help. The first suggestion ended up being the best option for my level of privileges.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now