Solved

DB2 - Select x number of rows

Posted on 1998-12-02
10
14,241 Views
Last Modified: 2012-05-04
How can I limit a select statement to x numbers of
rows returned. If this was Oracle I could say
select * from my_table where ROWNUM < 50
to get the first 49 rows. I want to do the same
thing with DB2.
0
Comment
Question by:mmoore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:odessa
ID: 1026049
In fact one way is by recursion

WITH B (CKEY,LVL)
(SELECT DISTINCT PKEY, 0
 FROM TROUBLE
 WHERE PKEY = 0
 UNION ALL
 SELECT C.CKEY, P.LVL +1
 FROM TROUBLE  
 WHERE PKEY NOT IN (SELECT CKEY FROM PARENT)
)
SELECT CKEY, LVL
FROM PARENT
WHERE LVL < 50;

HOPE it's helpful
0
 
LVL 3

Author Comment

by:mmoore
ID: 1026050
Wow, that is really wild! I don't quite understand what is happening. Could you
show me the example with table ORDER  and columns ORD_NUM and CUST_ID?
This would help me to see what is table data and what is IBM SQL
reserved words.
What is "WITH B (CKEY,LVL) "? Is this part of the syntax I would type in or is
it part of your explanation?
I am trying to use this with IBM's JDBC driver. I wonder if that will make a difference.
Thanks, this will be really helpful if it works!
Mike

0
 
LVL 7

Expert Comment

by:Dedushka
ID: 1026051
Hi, mmoore!

Try to use keyword "TOP" in you query, i.e.

SELECT TOP 50 MY_TABLE.*
FROM MY_TABLE;

Happy New Year!
Dedushka
0
Webinar: MariaDB® Server 10.2: The Complete Guide

Join Percona’s Chief Evangelist, Colin Charles as he presents MariaDB Server 10.2: The Complete Guide on Tuesday, June 27, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 
LVL 3

Author Comment

by:mmoore
ID: 1026052
Sorry, that dies not work. Gets "Unexpected token" when it
encounters the "50".

0
 
LVL 2

Expert Comment

by:odessa
ID: 1026053
No problem
WITH B (CKEY,LVL)
(SELECT DISTINCT PKEY, 0
 FROM TROUBLE
 WHERE PKEY = 0
 UNION ALL
 SELECT C.CKEY, P.LVL +1
 FROM TROUBLE  
 WHERE PKEY NOT IN (SELECT CKEY FROM PARENT)
)
SELECT CKEY, LVL , ORD_NUM, CUST_ID
FROM PARENT
WHERE LVL < 50;
ORDER BY ORD_NUM, CUST_ID

Now this is a recursion generated query "WITH B (CKEY,LVL) " is definition of temp Table B for internal use of recursion, about ODBC driver I don't now how it works
0
 
LVL 7

Expert Comment

by:Dedushka
ID: 1026054
Hi, mmoore!

Standard ANSI SQL include the LIMIT TO nn ROWS clause used to limit the number of rows returned by a query. Is DB2 support this standard?

Best regards.
Dedushka
0
 
LVL 3

Author Comment

by:mmoore
ID: 1026055
Hi again Dedushka!
I do not know if DB2 supports that standard.
I tried:
select * from iprov.rld_feature
and that works fine, so next I tried
select * from iprov.rld_feature limit to 10 rows
the result was " ... unexpected token ... "

Best Wishes,
Mike

0
 
LVL 3

Accepted Solution

by:
P_S_Price earned 50 total points
ID: 1026056
I have checked the DB2 Manuals for AS/400 and the IBM Implementation of SQL does not support 'TOP' or 'LIMIT'. The only way I can think that you can do this is to define a cursor for the select and then count the first N records retrieved in a program by the using the cursor.
0
 
LVL 3

Author Comment

by:mmoore
ID: 1026057
I can not really create a cursor because this is an ad-hoc SELECT statement using the JDBC interface. However I think that your answer about there being no built-in SQL syntax to accomplish the task is correct.
0
 

Expert Comment

by:N8DaGr8
ID: 7321914
Noticing the date on the original question, this might not have been a valid answer at the time, however for anyone who currently looks to this question in order to answer their current questions, it would be nice to post the correct answer.

In DB2 use a fetch first x rows only

So the query would be...
Select * from tablename where row='123' fetch first 50 rows only

Note, you can also say
Select * from tablename where row='123' fetch first 50 row only
Select * from tablename where row='123' fetch first 1 row only
Select * from tablename where row='123' fetch first 1 rows only
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question