wood burning stoves 2.0*
The moose likes Other Languages and the fly likes PL/SQL Snippet going to a infinite loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "PL/SQL Snippet going to a infinite loop" Watch "PL/SQL Snippet going to a infinite loop" New topic
Forums: Oracle/OAS Other Languages
Author

PL/SQL Snippet going to a infinite loop

Samrat Som
Ranch Hand

Joined: Feb 04, 2009
Posts: 40
I have problem in PL/SQL
Let me tell you , there is a table and I want to do the matching algorithm

Table Name : uf_sam_reassign_match
OLD_ID NEW_ID
1 1
2 1
2 2
3 2
4 1
4 3

Desired output should be in the table uf_sam_reassign_match_actual1

OLD_ID NEW_ID
1 1
2 2
4 3


The PL/SQL Snippet for the same is





But somehow at the row 4 of table uf_sam_reassign_match mentioned below its going to a infinite loop

Old_Id New_id
3 2


Please help me here.
I dont have any clue why its going to a infinite loop.


Thanks in Advance.

SCJP 1.6
SCWCD 1.5 (Preparing...)
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1784
    
  16

I've put this post in the Oracle forum as you may get more responses from Oracle people there. Also I formatted your code to make it easier to read.


No more Blub for me, thank you, Vicar.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1784
    
  16

In your inner loop on cursor d (you need to improve your naming conventions here), are you sure that the exit condition will always occur?

More generally, you would probably find it a lot easier to use a cursor FOR loop here. Here's a couple of examples based on your SQL:


chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1784
    
  16

If I understand your matching rule correctly, then you don't need two cursors or the nested loops here. You can use the data from your source table directly e.g. via a cursor FOR loop as shown above, provided you get the data in the right order. Then you want to insert each record into your target table in turn, but only if the NEW_ID does not already exist there.

Here is one way to do this kind of INSERT e.g. imagine that v_old_id and v_new_id are local variables in your PL/SQL code, and v_old_id = 3 and v_new_id = 2. What do you think will happen if a record already exists in the target table with NEW_ID = 2?

There are other ways to do this, but I reckon this should give you everything you need to implement your procedure.
Samrat Som
Ranch Hand

Joined: Feb 04, 2009
Posts: 40
Hi Chris,

Thanks a lot for takingout your precious time to resolve this issue.
actually your suggestion of forlop of cursor is good and readable.

there was an issue with my snippet and i have found it.
Basically after

Fetch d into v_old_id,v_new_id;
exit when d%notfound; -----> This was missing and therefore it was going to infinite loop
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1784
    
  16

Samrat Som wrote:exit when d%notfound; -----> This was missing and therefore it was going to infinite loop

Correct. That's why it's usually much easier to use a cursor FOR loop when you can: less hand-written code = less hand-written bugs!
 
 
subject: PL/SQL Snippet going to a infinite loop