title: Step out of a query to get where clauses from a separate table, within a stored procedure
body: <p>I have a procedure which I'm using to output row counts to a .csv file but some of the where clauses I may want to use are contained in a table. How can I use them to create conditions for the counts?</p>
  
  <p>I've attempted using concatenation pipes to select against the table that holds the where clauses but I'm confused about syntax and where they should go and I believe this is where I need the most help.</p>
  
  <p>These are the columns in the table that contains some of the where clauses I ultimately want to use in the procedure.</p>
  
  <pre><code>SCHEMA,  DATABASE,  FULL_TABLE,  DRIVER_TABLE,  MAND_JOIN
  </code></pre>
  
  <p>And the values may be such as:</p>
  
  <pre><code>PROD,  DB1,  RLTSHP,  BOB.R_ID,  A.AR_ID = B.AR_ID
  </code></pre>
  
  <p>The procedure I have written is as follows:</p>
  
  <pre><code>create or replace procedure PROJECT is
    --variables
    l_dblink varchar2(100) := 'DB1';
    ROW_COUNT number;
    file_handle UTL_FILE.file_type;
  BEGIN
    utl_file.put_line(file_handle, 'OWNER,TABLE_NAME,ROW_COUNT');
  
    --main loop
    for rws in (select /*+parallel */ owner, table_name
                  from dba_tables@DB1 a
                 where table_name in (select table_name
                                        from meta_table
                                       where driver_table is not null
                                         and additional_joins is null)
                   and a.owner in (select distinct schema
                                     from meta_table c)
                 order by table_name)
    loop
      execute immediate 'select count(*) from ' ||rws.owner||'.'||rws.table_name || '@' || l_dblink into ROW_COUNT;
      utl_file.put_line(file_handle,
                        rws.OWNER || ',' ||
                        rws.TABLE_NAME || ',' ||
                        ROW_COUNT);
    end loop;
  END PROJECT;
  /
  </code></pre>
  
  <p>However, instead of the simple <code>select count(*)</code> reflected in the above, I want to find a way to include data in the meta_table to construct "where" clauses that use table joins to limit the output so that I'm not counting all rows, but rows that meet the criteria in the join I've constructed. </p>
  
  <p>For example, so that the actual count that gets executed will be something like this:</p>
  
  <pre><code>select count(*)
    from PROD.RLTSHP@DB1 b,
         BOB.R_ID@DB1 a
   where A.AR_ID = B.AR_ID;
  </code></pre>
  
  <p>Essentially I would be constructing the query using the entries in the meta_table. I think I can do this with concat's / pipes but I'm not sure exactly how to.</p>
  
  <p>Can you help?</p>