Q
Problem solve Get help with specific problems with your technologies, process and projects.

How to limit the number of records in a cursor using DB2 native SQL?

How do I limit the number of records in a cursor using DB2 native SQL? I know how to do it in ODBC and JDBC.

The answer to your question is to use the FETCH FIRST n ROWS ONLY clause. This clause, new as of DB2 Version 7, is appended to the end of a SELECT statement to limit the actual number of rows selected to whatever value is supplied for "n". So, for example, to retrieve employee information but to limit the number of rows that can be returned to 5 or fewer, you could code:

SELECT FIRST_NAME, LAST_NAME, EMPNO
FROM EMP
FETCH FIRST 5 ROWS ONLY;

Even if there are more than five rows in the EMP table, only five will be returned by this SQL statement. Of course, fewer than five rows can be returned if there are less than five rows in the table.

This was last published in March 2003

Dig Deeper on IBM DB2 management

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchAWS

SearchContentManagement

SearchOracle

SearchSAP

SearchSQLServer

Close