Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PL/SQL Snippet going to a infinite loop

 
Samrat Som
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic