# Ruby + oracle last row

**URL:** <https://www.ruby-forum.com/t/ruby-oracle-last-row/98966>\
**Category:** Ruby\
**Created:** [July 19, 2006, 12:16pm UTC](https://www.ruby-forum.com/t/ruby-oracle-last-row/98966 "2006-07-19T12:16:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Faby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bbe5ce/32.png) [@Faby](https://www.ruby-forum.com/u/Faby)\
**Post date:** [July 19, 2006, 12:16pm UTC](https://www.ruby-forum.com/t/ruby-oracle-last-row/98966/1 "2006-07-19T12:16:57Z")

</div>

Hi all,  
I am new to ruby so any help will be greatly appreciated.  
I am using ruby with oracle, and the following text is an extract of my  
programm. (A cursor is fecthing the rows of a sql query.)

while row=cursor.fetch  
if(row[0]\<4)  
outputFile\<\<row[0].to\_s+"\t"+row[1].to\_s+"\n"  
elsif (row[0]\>=4)  
totalRow4+=row[1]  
end  
end  
What I need now is to be able to determine when my cursor has reached  
the last row of my query ie if row==last\_row…

I hope i explain myself clearly enough  
Thanks  
Faby

---

<div class="post-metadata">

**Author:** ![Faby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bbe5ce/32.png) [@Faby](https://www.ruby-forum.com/u/Faby)\
**Post date:** [July 19, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/ruby-oracle-last-row/98966/2 "2006-07-19T17:26:57Z")

</div>

Faby wrote:

> ```
> end
> 
> ```
> 
> end  
> What I need now is to be able to determine when my cursor has reached  
> the last row of my query ie if row==last\_row…
> 
> I hope i explain myself clearly enough  
> Thanks  
> Faby

Two ways come to mind:

1. first execute a count(\*) cursor to get the number of records,  
then in the while loop count the records so you know when  
you are on the last one.  
or similarly
2. Oracle tables have a psuedo-column called ROWNUM, which  
is a sequential number for a given records set. Execute a query  
to get the max(ROWNUM) for the query results, then in the while  
loop check for the row.ROWNUM equal to maxRowNum.

I also believe you can do a fetch\_all to get all rows into an array,  
but  
this will depend on how much memory the data needs…

Cheers

---

<div class="post-metadata">

**Author:** ![Faby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bbe5ce/32.png) [@Faby](https://www.ruby-forum.com/u/Faby)\
**Post date:** [July 20, 2006, 6:36am UTC](https://www.ruby-forum.com/t/ruby-oracle-last-row/98966/3 "2006-07-20T06:36:59Z")

</div>

Faby wrote:

> ```
> end
> 
> ```
> 
> end

When I need a look-ahead I sometimes do something like this:

nextrow = cursor.fetch  
while row=nextrow  
nextrow = cursor.fetch  
if nextrow.nil?  
p row #print last row  
end  
end

Daniel

---

<div class="post-metadata">

**Author:** ![Faby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bbe5ce/32.png) [@Faby](https://www.ruby-forum.com/u/Faby)\
**Post date:** [July 20, 2006, 10:42am UTC](https://www.ruby-forum.com/t/ruby-oracle-last-row/98966/4 "2006-07-20T10:42:35Z")

</div>

Hi,Thank you for your answers. I managed to get my problem solved using  
a count method. Thanks Daniel for your answer, i will try to implement  
it now as it may be a quicker way.  
Many thanks for your help  
Faby
