Using results of a cursor in another cursor

Posted on 2008-06-26
Medium Priority
Last Modified: 2013-12-18

I'm looking at creating a procedure that will allow me to check to see if data has changed in a column from staging table compared with the reporting table. Yep simple

select * from stage_tbl s
where exists (select 1 from report_tbl r
                       where s.id = r.id
                       and s.compare_col_a <> r.compare_col_a
but .... I have a number of columns to compare between the two table and I can't rely on the columns having the same name.  So I have created a mapping table that consists of the column names between the two tables.  Though how can I make 'and s.compare_col_a <> r.compare_col_a' dynamic so it accepts the other compare columns such as 'and s.compare_col_b <> r.compare_col_b'

Your help would certainly be appreciated
Question by:alines
LVL 15

Expert Comment

ID: 21876856
if you need such procedure for various tables dynamic query might make sense, however it is much slower than direct query.
So I would recommend to select records from you mapping table once to construct this query and then make static SQL statement
select ' and ' || first_table_field || ' <> ' || second_table_field from mapping table.

If you nevertheless need dynamic SQL, you can use the same statement in cursor and construct the query dynamically in your procedure
LVL 22

Expert Comment

ID: 21877857
    If you just want to know which records contain a change and you don't need to know WHICH column, then you could use the minus (assuming the columns are in the same order or can be arranged to be in the same order).  Of course, you need to make sure it's only when the id exists, but that can be done with a simple IN.  Set operations are pretty fast:

create table updates_to_process as
select * from staging_table
 where id in
        (select id from reporting_table)
select * from reporting_table;

ANY change in the data will result in the MINUS not trimming that record from the set going to the "updates_to_process".

Good luck!
LVL 15

Expert Comment

ID: 21881231
Are you looking for something like this...

create or replace procedure StgRptCompare(inStgTable in varchar2, inRptTable in varchar2)
  type cur_ref is ref cursor;
  cursor cColList is select colA, colB from table_map where <<some condition>>;
  cr cur_ref;
  sql_qry    varchar2(4000);
  sql_qry := 'select * from ' || inStgTable || ' s ' ||
             'where exists (select 1 from ' || inRptTable || ' r ' ||
             '              where s.id = r.id ';
  for c1 in cColList loop
    sql_qry := sql_qry || 'and s.' || c1.colA || ' <> r.' || c1.colB || ' ';
  end loop;
  sql_qry := sql_qry || ')';
  open cr for sql_qry;

Open in new window

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.


Author Comment

ID: 21882299
Hi Ishando

I'm pretty sure this is what I'm looking for.  Will it allow me to create a cursor that will loop each of the compare columns 'c1.colA || ' <> r.' || c1.colB ' so that any data that is returned can then be incorporated into an insert statement.


LVL 22

Accepted Solution

DrSQL earned 2000 total points
ID: 21882971
    Other than the fact that it is using a negative join, rather than a set operation (so it will perform worse), this has the same limitations as what I proposed - you don't know the column that caused the record to be selected.  If your issue with the MINUS approach is that you won't use every column, then just generate those:

create or replace procedure GrabDifferences(StagingTable varchar2, ReportTable varchar2) is
  Type varchartable is table of varchar2(2000);
  StageColumns varchartable;
  ReportColumns varchartable;
  StageSelect varchar2(2000);  -- you may need this to be longer
  ReportSelect varchar2(2000);
  StagePK varchar2(30);
  ReportPK varchar2(30);
  this number;

    select column_name bulk collect into StageColumns
       from table_map
     where table_name=StagingTable
     order by column_position;

    select column_name bulk collect into ReportColumns
       from table_map
     where table_name=ReportTable
     order by column_position;

    select column_name into StagePK from table_map where table_name=StagingTable and PK='Y';
    select column_name into ReportPK from table_map where table_name=ReportTable and PK='Y';

    for this in StageColumns.first..StageColumn.Last loop
        StageSelect := StageSelect || ',' || StageColumns(this);
        ReportSelect := ReportSelect || ',' || ReportSelect(this);
   end loop;

   execute immediate
            'insert into '||StagingTable|| '_changes ' ||
                'select ' || ltrim(StageSelect,',') || ' from ' || StagingTable ||
                '  where ' || StagePK || ' in (select ' || ReportPK || ' from ' || ReportTable || ')' ||
                ' minus' ||
                ' select ' || ltrim(ReportSelect,',') || ' from ' || ReportTable ;

If you want to know which column(s) is/are responsible for the difference you'll need to pivot the table.  Let us know if that's the goal.

Good luck!

Author Closing Comment

ID: 31471034
DrSQL .... Thanks for your help.  Why is it the simplest solutions are always the best.

Also thanks to everyone else who replied

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

627 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