473,396 Members | 2,050 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,396 software developers and data experts.

How to move backwards

I have a problem regarding PL/SQL. I want to know how to move backwards while scanning a cursor:

The prototype of my procedure is as follows:

Expand|Select|Wrap|Line Numbers
  1. create or replace procedure xxx is
  2.       cursor c1 is select thmaster.rowid, thmaster.* from thmaster;
  3.       num_recs number;
  4.       prev_txn number;
  5.       curr_txn number;
  6. begin
  7.       prev_txn:=0;
  8.       for r1 in c1
  9.       loop
  10.            curr_txn:=r1.txncntr;
  11.            if curr_txn<>prev_txn+1 then
  12.                  -- Here I want to fetch previous record and update some field of that record.                 
  13.                  commit;
  14.           end if;
  15. --
  16.           prev_txn:=curr_txn;
  17.       end loop;
  18. end;
In the if statement above, I want to fetch previous record and update some field of that record. But I dont seem to find any syntax for that. Actually, I am looking for something like
Expand|Select|Wrap|Line Numbers
  1. recordset.moveprevious 
method of Visual Basic.

Thanks in anticipation.
Jan 12 '08 #1
7 6947
debasisdas
8,127 Expert 4TB
there is no way you can go back using oracle cursor. there is no such method or facility.
Jan 12 '08 #2
there is no way you can go back using oracle cursor. there is no such method or facility.
It may not be in your knowledge, but one thing I can bet for is that there must be something like that to achieve my requirement. Beacuse it is not a very rare kind of requirement. It is generally needed for day-to-day programming.

May be I will have to search somewhere else.
Jan 12 '08 #3
Dave44
153 100+
It may not be in your knowledge, but one thing I can bet for is that there must be something like that to achieve my requirement. Beacuse it is not a very rare kind of requirement. It is generally needed for day-to-day programming.

May be I will have to search somewhere else.
Yes, there is always more than one way to skin a cat ;)

In your algorithm, will it be the most previous row that you want? In oracle there isnt a way to start looping through a cursor record set and then suddently reverse direction and loop back again.

But there are alternatives or other ways. How do you define the order of the record set you want to loop through? See if you suddenly wanted to reverse direction it would indicate that you reached some specific point that indicated such a need. I dont see an order by in your cursor. the reason i want to know is that we can use analytic functions like lag() or row_Number() or rank() to order the rows a specific way.
For example lag() can be used to return the previous rows value of a certain field based upon some order defined. likewise lead() will return the next rows value of a certain field based upon a sort order given.

can you give a little more info on what it is you need to do?
Jan 13 '08 #4
debasisdas
8,127 Expert 4TB
It may not be in your knowledge, but one thing I can bet for is that there must be something like that to achieve my requirement. Beacuse it is not a very rare kind of requirement. It is generally needed for day-to-day programming.

May be I will have to search somewhere else.
I would really appreceate ,if you can post the answer and a way to move backwards in oracle cursor.
Jan 14 '08 #5
amitpatel66
2,367 Expert 2GB
You have almost got the solution of accessing or refering to the previous record by selecting a rowid in your cursor. what you just need to do further is this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> ed
  3. Wrote file afiedt.buf
  4.  
  5.   1  declare
  6.   2  cursor c1 is select rowid,emp_det.* from emp_det ORDER BY emp_det.empno;
  7.   3  empno_prev NUMBER := 0;
  8.   4  empno_curr NUMBER := 0;
  9.   5  empd emp_det%ROWTYPE;
  10.   6  prev_rowid VARCHAR2(100) := NULL;
  11.   7  begin
  12.   8  FOR I IN C1 LOOP
  13.   9  empno_curr:= I.empno;
  14.  10  IF empno_curr <> empno_prev + 1 THEN
  15.  11  SELECT x.* INTO empd FROM emp_Det x where rowid = prev_rowid;
  16.  12  dbms_output.put_line('Previous Empno:'||empd.empno||' Current Empno:'|| I.empno);
  17.  13  ELSE
  18.  14  dbms_output.put_line('current and prev trans equal');
  19.  15  END IF;
  20.  16  prev_rowid := I.rowid;
  21.  17  END LOOP;
  22.  18* END;
  23. SQL> /
  24. current and prev trans equal
  25. Previous Empno:1 Current Empno:2
  26. Previous Empno:2 Current Empno:3
  27. Previous Empno:3 Current Empno:4
  28. Previous Empno:4 Current Empno:5
  29. Previous Empno:5 Current Empno:6
  30. Previous Empno:6 Current Empno:12
  31. Previous Empno:12 Current Empno:17
  32. Previous Empno:17 Current Empno:20
  33. Previous Empno:20 Current Empno:211
  34. Previous Empno:211 Current Empno:1002
  35.  
  36. PL/SQL procedure successfully completed.
  37.  
  38. SQL> 
  39.  
  40.  
We cannot play with the cursor the way you want to in oracle but the above approach can be an alternative to what you require. Make use of ROWID and get your results.

I hope this helps!!
Jan 14 '08 #6
You have almost got the solution of accessing or refering to the previous record by selecting a rowid in your cursor. what you just need to do further is this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> ed
  3. Wrote file afiedt.buf
  4.  
  5.   1  declare
  6.   2  cursor c1 is select rowid,emp_det.* from emp_det ORDER BY emp_det.empno;
  7.   3  empno_prev NUMBER := 0;
  8.   4  empno_curr NUMBER := 0;
  9.   5  empd emp_det%ROWTYPE;
  10.   6  prev_rowid VARCHAR2(100) := NULL;
  11.   7  begin
  12.   8  FOR I IN C1 LOOP
  13.   9  empno_curr:= I.empno;
  14.  10  IF empno_curr <> empno_prev + 1 THEN
  15.  11  SELECT x.* INTO empd FROM emp_Det x where rowid = prev_rowid;
  16.  12  dbms_output.put_line('Previous Empno:'||empd.empno||' Current Empno:'|| I.empno);
  17.  13  ELSE
  18.  14  dbms_output.put_line('current and prev trans equal');
  19.  15  END IF;
  20.  16  prev_rowid := I.rowid;
  21.  17  END LOOP;
  22.  18* END;
  23. SQL> /
  24. current and prev trans equal
  25. Previous Empno:1 Current Empno:2
  26. Previous Empno:2 Current Empno:3
  27. Previous Empno:3 Current Empno:4
  28. Previous Empno:4 Current Empno:5
  29. Previous Empno:5 Current Empno:6
  30. Previous Empno:6 Current Empno:12
  31. Previous Empno:12 Current Empno:17
  32. Previous Empno:17 Current Empno:20
  33. Previous Empno:20 Current Empno:211
  34. Previous Empno:211 Current Empno:1002
  35.  
  36. PL/SQL procedure successfully completed.
  37.  
  38. SQL> 
  39.  
  40.  
We cannot play with the cursor the way you want to in oracle but the above approach can be an alternative to what you require. Make use of ROWID and get your results.

I hope this helps!!

Thanks Amit. It really did help me. This is exactly I was looking for.
Jan 14 '08 #7
amitpatel66
2,367 Expert 2GB
Thanks Amit. It really did help me. This is exactly I was looking for.
You are welcome :)

Happy programming :)
Jan 14 '08 #8

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

Similar topics

1
by: Jason Mobarak | last post by:
Greetings! Say that it's desirable to provide backwards compatibility for methods of an object, consider the case where... class Foo: def bar (self, a, b): pass ....is a defined class...
0
by: flat_ross | last post by:
If there was one thing nice about "Binary Compatibility" in VB6, it would tell you at compile time that you changed your public interface. I am looking for the same functionality in .NET. I know...
7
by: Sonny | last post by:
I need to port a library that is written entirely in C to C++. The library is supported on quite a few platforms (windows, Solaris, Linux, AIX, HP-UX, OSX, etc...) and there's quite an existing...
15
by: SK | last post by:
Hey folks, I am searching for a string (say "ABC") backwards in a file. First I seek to the end. Then I try to make a check like - do { file.clear (); file.get(c); file.seekg(-2,...
0
by: Svelte Poshy | last post by:
I want to put a code in the control box with a picture of an arrow, imitating the right arrow in the foot of the form with which the user views the customers one after another. My form is a...
1
by: wolftor | last post by:
I am writing some strings to MS Word. When I use the str function to convert the street number to a text string, I believe it adds a space in front for +/-. I don't want a space at the start of...
6
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
70
by: py | last post by:
I have function which takes an argument. My code needs that argument to be an iterable (something i can loop over)...so I dont care if its a list, tuple, etc. So I need a way to make sure that...
4
by: sara | last post by:
I have an A2K database that has links to 3 of my other databases (external links) to run some reports. The coding in any of the 4 is pretty simple. Recently when we make a new .mde for the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.