473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Looping over broken range

4
Hi,

In PL/SQL (Oracle 9i) I need to iterate over a discontinuous range of patient-ID's (numbers).
I need something like

WHILE variable IN (SELECT DISTINCT ....)
LOOP...

However Oracle does not allow using a subquery in this way.
I know in version 11 there is the CONTINUE construct but we do not run 11...
Does anyone have suggestions on how to do this in an other way?

thanks,
Willem
Mar 12 '08 #1
7 2193
amitpatel66
2,367 Expert 2GB
Do you mean patient_id can be 1,3,5,7,8,10,11 and so on and not continuous??
Mar 12 '08 #2
wwlos
4
Do you mean patient_id can be 1,3,5,7,8,10,11 and so on and not continuous??
Yes, indeed, that's it.
Willem
Mar 13 '08 #3
amitpatel66
2,367 Expert 2GB
Yes, indeed, that's it.
Willem

Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> SET SEVRVEROUTPUT ON
  3.  
  4. SQL>declare
  5. cursor C1 is SELECT patient_id FROM table_name;
  6. BEGIN
  7. FOR I IN C1 LOOP
  8. DBMS_OUTPUT.PUT_LINE(I.patient_id);
  9. END LOOP;
  10. END;
  11. /
  12.  
  13.  
Mar 13 '08 #4
wwlos
4
Thanks. I tried the cursor loop before, but I could not get it to work.
The code below does exactly what I want... as long as there is a continuous patient range (Current_pat). As soon as there is a gap, processing stops and I get the "no data found" message.
Maybe this is getting too specific to be discussed here.. in which case I humbly apologize.

Thanks again,
Willem

The rest is...

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare
  3. TYPE rec_data IS RECORD
  4. (as11 number, as12 number, as13 number, as14 number,
  5. as21 number, as22 number, as23 number, as24 number,
  6. as31 number, as32 number, as33 number, as34 number
  7. );
  8. gegs rec_data;
  9. First_Pat number;
  10. Last_Pat number;
  11. Current_Pat number;
  12. teller number;
  13.  
  14. begin
  15. select min(seq_med_diagnose) into First_Pat from med_diagnose_as;
  16. select max(seq_med_diagnose) into Last_Pat from med_diagnose_as;
  17.  
  18. Current_pat:= First_Pat;
  19.  
  20. while Current_pat between First_Pat and Last_Pat
  21. loop
  22.  
  23. select * into gegs
  24. from (select *
  25. from (select nvl(seq_alg_dsm_iv , 99999) "as11"
  26. , nvl(lead(seq_alg_dsm_iv,1) over (order by seq_MED_diagnose_as), 99999) "as12"
  27. , nvl(lead(seq_alg_dsm_iv,2) over (order by seq_MED_diagnose_as), 99999) "as13"
  28. , nvl(lead(seq_alg_dsm_iv,3) over (order by seq_MED_diagnose_as), 99999) "as14"
  29. from med_diagnose_as
  30. where seq_med_diagnose = Current_Pat
  31. and hc_as = 1
  32. and rownum between 1 and 4
  33. )
  34. where rownum=1
  35. )
  36. , (select *
  37. from (select nvl(seq_alg_dsm_iv , 99999) "as21"
  38. , nvl(lead(seq_alg_dsm_iv,1) over (order by seq_MED_diagnose_as), 99999) "as22"
  39. , nvl(lead(seq_alg_dsm_iv,2) over (order by seq_MED_diagnose_as), 99999) "as23"
  40. , nvl(lead(seq_alg_dsm_iv,3) over (order by seq_MED_diagnose_as), 99999) "as24"
  41. from med_diagnose_as
  42. where seq_med_diagnose = Current_Pat
  43. and hc_as = 2
  44. and rownum between 1 and 4
  45. )
  46. where rownum=1
  47. )
  48. , (select *
  49. from (select nvl(seq_alg_dsm_iv , 99999) "as31"
  50. , nvl(lead(seq_alg_dsm_iv,1) over (order by seq_MED_diagnose_as), 99999) "as32"
  51. , nvl(lead(seq_alg_dsm_iv,2) over (order by seq_MED_diagnose_as), 99999) "as33"
  52. , nvl(lead(seq_alg_dsm_iv,3) over (order by seq_MED_diagnose_as), 99999) "as34"
  53. from med_diagnose_as
  54. where seq_med_diagnose = Current_Pat
  55. and hc_as = 3
  56. and rownum between 1 and 4
  57. )
  58. where rownum=1
  59. )
  60. ;
  61.  
  62. Current_pat:= Current_pat+1;
  63.  
  64. dbms_output.put_line(Current_Pat || ',' || gegs.as11 || ',' || gegs.as11 || ',' || gegs.as12 || ',' || gegs.as13 || ',' || gegs.as14 || ',' ||
  65. gegs.as21 || ',' || gegs.as22 || ',' || gegs.as23 || ',' || gegs.as24 || ','||
  66. gegs.as31 || ',' || gegs.as32 || ',' || gegs.as33 || ',' || gegs.as34 );
  67. end loop;
  68. end;
  69.  
Mar 14 '08 #5
amitpatel66
2,367 Expert 2GB
Ofcourse that will happen becuase you are taking MIN and MAX values and if any value between MIN and MAX does not exist then it will result in NO DATA FOUND. You need to reimplement your code using cursor that way I showed you in my previous post. Try and post back what error you get using Cursor
Mar 14 '08 #6
wwlos
4
Hi,

Sorry for not responding until now. I have been ill for a while.
The problem has been solved by our application provider in the latest release. I haven't got the time to work on this problem any longer.

Thank you for your help,
Regards,
Willem
Mar 25 '08 #7
amitpatel66
2,367 Expert 2GB
Hi,

Sorry for not responding until now. I have been ill for a while.
The problem has been solved by our application provider in the latest release. I haven't got the time to work on this problem any longer.

Thank you for your help,
Regards,
Willem
Thats good that problem is resolved. ANyways, do post back when ever you have any further issues.
Mar 25 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: drife | last post by:
Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? In Perl this construct...
6
by: Mike | last post by:
Hello, This game will not loop. It compiles, builds, and executes fine, but the game will not loop around no matter what I do. What is wrong with the code? What do I have to add to make it loop...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
14
by: John Salerno | last post by:
Here's an exercise I was doing to guess a number from 1-100. Just for fun (ha ha) I decided to add some error checking too, and now when I run it, the DOS prompt flashes real quick and disappears....
8
by: Tommy Grav | last post by:
I have a list: a = I want to loop over a and then loop over the elements in a that is to the right of the current element of the first loop In C this would be equivalent to:
14
by: NetworkElf | last post by:
Hi all, Does anyone have some code that shows an example of how to loop through a range of IP addresses? I'm using text boxes to get a start and end value for the range. I was thinking about...
3
by: assgar | last post by:
Hi I am having problem with my loping. I don't know if I have chosen the correct approach. GOAL: I need to insert into a table event types for a specific date range. The calendar the event...
3
by: richie9648 | last post by:
Hi All I have created a macro which turns reports done by users into a format that is needed by me. The good this is that all the reports are in the same format. However my question is around...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.