If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > how to Compare 2 tables using cursors??

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-19-03, 08:04
moza moza is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Unhappy how to Compare 2 tables using cursors??

I have 2 tables that have the same fields and the same data. However when I counted the records, I found that one table contain more records than the other. Now I need to delete the extra records and make the table identical. How can I do this using cursor?

Please advice it's urgent
Reply With Quote
  #2 (permalink)  
Old 10-19-03, 15:24
cvandemaele cvandemaele is offline
Registered User
 
Join Date: Oct 2003
Location: Switzerland
Posts: 140
First of all, are you sure you only have extra records in the second (or first, whatever) table ?

Indeed, if table A contains the followings rows
1
2
3

and table B contains
1
2
4
5

then deleting all "extra" rows in table B will end you up with 2 rows in table B, which is not exactly what you want...

Don't bother using a cursor when you can do things directly :

DELETE FROM B WHERE b.pk NOT IN (SELECT a.pk FROM A)

where pk stands for primary key (or some unique key).

Anyway, to make two tables identical, i would just truncate the "bad" one, and re-insert all rows from the "good" one.
Reply With Quote
  #3 (permalink)  
Old 10-19-03, 23:39
moza moza is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Thank You for your reply ..

The problem that it's not that easy. One table is in Oracle Platform and the other is in as400 (IBM platform) but I took care of that. The other problem is that the oracle table contain around 87000 records and as400 table has 84000 although we are running a batch file to copy the same records to both tables. The normal way of doing this will take at least 1 hour to run the query. So I tought cursors will make it faster.

What do you think?

Last edited by moza; 10-19-03 at 23:42.
Reply With Quote
  #4 (permalink)  
Old 10-20-03, 02:36
cvandemaele cvandemaele is offline
Registered User
 
Join Date: Oct 2003
Location: Switzerland
Posts: 140
I'm still trying to understand. Can you confirm following assumptions ?

1. Your "source" table is the one on the AS400, and it contains all data you need.

2. Your "duplicate" table is the one on Oracle, and it contains too many rows.

3. Now, you would like to "restore" the table on Oracle using data from your table on the AS400.

CVM.
Reply With Quote
  #5 (permalink)  
Old 10-21-03, 06:58
moza moza is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
Yes this is exactly what I need and note that there are 4 PK for the tables. What I found out that sometimes when the user delete a record from the as400 table it's not deleted in oracle. that's why we have more records in oracle.

the system we have is basically like this

as400 master table : store original data

as400 intermediate file : takes the data from the master table and store them

oracle intermediate file: take the information from the as400 intermediate file and store them

oracle master table: takes the information from oracle intermediate table.

so at the end the as400 master table and the oracle master table must be identical however they are not.
Reply With Quote
  #6 (permalink)  
Old 10-21-03, 07:10
cvandemaele cvandemaele is offline
Registered User
 
Join Date: Oct 2003
Location: Switzerland
Posts: 140
Quote:
Originally posted by moza
The normal way of doing this will take at least 1 hour to run the query. So I tought cursors will make it faster.
Now that I've got your answers, let's get back to your original problem.

1. Of course you can do it using cursors (read from as400, see if it exists on Oracle, ...). This is quite straightforward to implement, but it will not be faster than copying the data directly (see point 2). Moreover, you need to write code (and we don't like that, do we...)

2. My current project also involves an AS400 (source) and an Oracle database (destination). I cannot imagine that those 87000 rows are giving you a hard time. I don't know the size of a row, nor do I know about the number of indexes, etc. that are linked to the destination table, but I can simply not imagine that it takes about 1 hour. Did you try using the (Sql*Plus) command COPY FROM ?
Reply With Quote
  #7 (permalink)  
Old 10-21-03, 08:56
moza moza is offline
Registered User
 
Join Date: Oct 2003
Posts: 4
I know that there are some simple ways of doing this, but it's not dicision to make. Moreover, these table are so active and users are accessing it every seconds so there should be no chance for any mistake or loss or data. What my boss suggested is to run cursors for both tables and store as oracle record in local variables, search for it in as400, if not exists then delete it. But It's not working.
The tables doesn't use indexes.
Reply With Quote
  #8 (permalink)  
Old 10-21-03, 10:17
cvandemaele cvandemaele is offline
Registered User
 
Join Date: Oct 2003
Location: Switzerland
Posts: 140
What exactly do you mean by "The tables doesn't use indexes" ?

Anyway, I'm a getting out of ideas here. Knowing a little bit about the AS400, and considering the fact that you don't want to have mistakes/loss or data during your patch, my guess is you need to run your patch using a very high commitment control level (on the AS400) like REPEATABLE READ, which gets your users locked anyway.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On