cursor record next peek
 
Sponsored Links

cursor, record, next, peek


cursor, record, next, peek

I have a procedure that uses a cursor to process the record in a table serially from beginning to end. Some of the logical records span more than one row and I need a way to peek  at the next record with out changing the position of my cursor.

I was thinking of something like:
Select * into new_record from table where rowid = rowid_of_current_record + 1

Can I use rowid to do this?   Is there a better way?


A:
rowid = rowid_of_current_record + 1

Rowids are not sequential. And the above expression doesnt guatantee any order of your result set. Each record in your table gets assigned to a new rowid as an when the record goes into the table. So, if you code a program to peek into the next record using ROWID, the program may look into one record today. and if the record gets deleted tomorrow, your program will peek into another record tomorrow.

So, what exactly is the problem you are trying to solve here.

>> peek at the next row, what are the columns that decides the next row you want to peek at?

Now, you can use the LEAD operator to solve this problem.

Have a look at this example:

SQL> select * from tbl2 order by id;

       ID VAL
---------- --------------------
       20 two
       40 four
       50 five
       70 seven
      100 ten

SQL> select id, val,
 2  lead(id, 1) over(order by id) next_id,
 3  lead(val, 1) over(order by id) next_val
 4  from tbl2
 5  /

       ID VAL                     NEXT_ID NEXT_VAL
---------- -------------------- ---------- --------
       20 two                          40 four
       40 four                         50 five
       50 five                         70 seven
       70 seven                       100 ten
      100 ten


You can also use ROWID in this logic(but make sure that you get the correct results).

SQL> select * from tbl2 order by rowid;

       ID VAL
---------- --------------------
      100 ten
       20 two
       40 four
       50 five
       70 seven

SQL> select id, val,
 2  lead(id, 1) over(order by rowid) next_id,
 3  lead(val, 1) over(order by rowid) next_val
 4  from tbl2
 5  /

       ID VAL                     NEXT_ID NEXT_VAL
---------- -------------------- ---------- -----------
      100 ten                          20 two
       20 two                          40 four
       40 four                         50 five
       50 five                         70 seven
       70 seven

   
Page  1
 
Menu
Free IT EBooks (1075)
Free .NET Ebooks (84)
Free ActionScript Ebooks (10)
Free ADO.NET Ebooks (6)
Free Adobe Ebooks (17)
Free AI Ebooks (56)
Free Ajax Ebooks (17)
Free Ant Ebooks (12)
Free Apache Ebooks (29)
Free ASP Ebooks (5)
Free ASP.NET Ebooks (30)
Free Assembly Ebooks (6)
Free AutoCAD Ebooks (5)
Free Blogging Ebooks (5)
Free CCNA Ebooks (2)
Free CGI Ebooks (2)
Free Cisco Ebooks (22)
Free Cocoa Ebooks (4)
Free Cryptography Ebooks (9)
Free CSharp Ebooks (25)
Free CSS Ebooks (11)
Free CVS Ebooks (3)
Free Database Ebooks (163)
Free DB2 Ebooks (1)
Free Delphi Ebooks (3)
Free DHTML Ebooks (2)
Free Directx Ebooks (3)
Free DNS Ebooks (6)
Free Dreamweaver Ebooks (23)
Free Eclipse Ebooks (13)
Free EJB Java Ebooks (1)
Free Firewall Ebooks (24)
Free Flash Ebooks (12)
Free Forensic Ebooks (5)
Free FreeBSD Ebooks (13)
Free Game Ebooks (13)
Free Graphic Ebooks (6)
Free Hack Ebooks (102)
Free Hardware Ebooks (6)
Free Hibernate Ebooks (7)
Free IPSec Ebooks (5)
Free iptables Ebooks (3)
Free IPv6 Ebooks (3)
Free J2EE Ebooks (5)
Free Java 1.4 Ebooks (5)
Free Java 1.5 Ebooks (2)
Free Java Ebooks (218)
Free Java RMI Ebooks (2)
Free Java Web Services Ebooks (5)
Free JavaBeans Ebooks (8)
Free Javascript Ebooks (62)
Free JavaServer Ebooks (8)
Free JBoss Ebooks (6)
Free joomla Ebooks (46)
Free JSP Ebooks (1)
Free Jython Ebooks (1)
Free LDAP Ebooks (8)
Free Linux Ebooks (281)
Free MacOS Ebooks (1)
Free Macromedia Ebooks (8)
Free Maya Ebooks (5)
Free MCDBA Ebooks (1)
Free MCSE Ebooks (1)
Free Misc Ebooks (2)
Free MySql Ebooks (48)
Free Network Ebooks (141)
Free OpenBSD Ebooks (13)
Free Oracle Ebooks (84)
Free Perl Ebooks (29)
Free Photoshop Ebooks (19)
Free PHP Ebooks (62)
Free PHPMySQL Ebooks (1)
Free PostgreSQL Ebooks (15)
Free Programming Ebooks (18)
Free Python Ebooks (43)
Free Regular Expression Ebooks (1)
Free RHCE Ebooks (3)
Free Ruby Ebooks (12)
Free Samba Ebooks (3)
Free Shell Ebooks (60)
Free Snmp Ebooks (2)
Free SOAP Ebooks (2)
Free Solaris Ebooks (21)
Free SQL Ebooks (47)
Free Squid Ebooks (17)
Free Struts Ebooks (3)
Free Subversion Ebooks (4)
Free TCPIP Ebooks (2)
Free TestKing Ebooks (2)
Free Tomcat Ebooks (7)
Free UML Ebooks (3)
Free Unix Ebooks (44)
Free VB Ebooks (3)
Free VB.NET Ebooks (6)
Free VBScript Ebooks (1)
Free Visual Basic Ebooks (31)
Free VoIP Ebooks (1)
Free VPN Ebooks (4)
Free WebDesign Ebooks (1)
Free Weblogic Ebooks (3)
Free Windows Ebooks (107)
Free XML Ebooks (26)
Free XSLT Ebooks (1)
tags



Bookmark and Share
site
site