MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

db2 - row fetching

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
sendeepr
Beginner


Joined: 26 Sep 2003
Posts: 4
Topics: 2

PostPosted: Fri Sep 26, 2003 6:16 am    Post subject: db2 - row fetching Reply with quote

Hi

1. I need only the first 2 rows to be fetched from a table which is already sorted ?

2. I need the 997 row to be fetched from a sorted table of 1000 rows ?
Back to top
View user's profile Send private message
SureshKumar
Intermediate


Joined: 23 Jan 2003
Posts: 211
Topics: 21

PostPosted: Fri Sep 26, 2003 7:01 am    Post subject: Reply with quote

1. Are you refering to DB2 ? what do you mean by 'already sorted', i assume you have an order by clause. If using V7 - FETCH FIRST 2 ROWS ONLY.
2. I am not sure about this, why not reverse the sort order and pick the first three rows from top, discard the first two and you have the 997th row.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Sep 26, 2003 8:35 am    Post subject: Reply with quote

sendeepr,

Good answers Suresh Kumar.

Fetch FIRST N Rows is a new feature of DB2 Version 7. so if your shop does not have DB2 version 7 then you can define a cursor with ORDER BY in the program and fetch it just once.

For the second question define the cursor with ORDER BY KEY DESC and fetch it 4 times and get the 997th record.

Hope this helps...

cheers

kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
naren_ab
Beginner


Joined: 07 Jan 2003
Posts: 32
Topics: 10

PostPosted: Fri Sep 26, 2003 11:36 am    Post subject: Reply with quote

Hi all, I agree with Kolusu and suresh as long as the data is not large, and
you don't have need for where clause and i think sendeepr has small data.

However i have similar problem, yet to find solution. My table is as large as 10 mi rows. we have ried this solution like month back and it costed us a lot on DB2. the problem is when you do order by desc it is doing tablespace scan. In case of order by asc it knows to follow the indexes.
examples

Select ID from tableA
where ID >= 0
fetch first 100 rows only
order by ID;

Select ID from tableA
where ID <= 99999999
fetch first 100 rows only
order by ID DESC; the only solution we have in hand is to know one more value for the key so that we can between and save db2 cost.
Back to top
View user's profile Send private message
SureshKumar
Intermediate


Joined: 23 Jan 2003
Posts: 211
Topics: 21

PostPosted: Fri Sep 26, 2003 2:58 pm    Post subject: Reply with quote

Thanks Naren, that was a good catch. I should have cautioned Sandeepr about the table size. Hopefully, V8 has promised to takecare by allowing a backwards scan on an index. Thanks
Back to top
View user's profile Send private message
sendeepr
Beginner


Joined: 26 Sep 2003
Posts: 4
Topics: 2

PostPosted: Mon Sep 29, 2003 6:19 am    Post subject: Reply with quote

hi all

thank u very much for the replies ...
Q1 is ok
Q2 i need only the 4th or 997th row alone to be retrieved....pls clarify...?

anyway happy for the response
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Mon Sep 29, 2003 8:16 am    Post subject: Reply with quote

Sendeepr,

Did you read all the answers clearly?? The second question of yours is also answered.Please read the posts once again

Thanks

kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sendeepr
Beginner


Joined: 26 Sep 2003
Posts: 4
Topics: 2

PostPosted: Tue Sep 30, 2003 8:13 am    Post subject: Reply with quote

ok i did

thankx

can u explain the limitations of using the LIKE in DB2 ....it is not that easy...
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Tue Sep 30, 2003 11:04 am    Post subject: Reply with quote

sendeepr,

The limitations of LIKE predicate depends on whether it is indexable or not.


    [a]. If you code COL LIKE 'pattern' then it is indexable except if a field procedure exists on that column.

    . If you code [b]COL NOT LIKE ' char', then it is NOT indexable.

    [c]. If you code COL LIKE '%char' , then it is NOT indexable.It is Indexable only if an ESCAPE character is specified and used in the LIKE predicate. For example, COL LIKE '+%char' ESCAPE '+' is indexable. This rule is not applicable if a field procedure exists on that column.

    [d]. If you code COL LIKE '_char' same rule as C.
    [e]. If you code COL LIKE host variable, then it is indexable only if the pattern in the host variable is an indexable constant (for example, host variable='char%'). This rule is not applicable if a field procedure exists on that column.



check this link for a detailed explanation of All you want to know about LIKE predicate

Hope this helps...

cheers

kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group