?
Solved

DB2 - Select x number of rows

Posted on 1998-12-02
10
Medium Priority
?
14,246 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 150 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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