How to Use Variables to Limit the Number of Rows in DB2
The IBM DB2 database solution uses SQL syntax to query the database, but it uses a unique syntax to limit the number of rows returned. In IBM DB2 syntax, you must append the number of rows you want to display at the end of the query statement, which is not typical of most database programs. You use the "fetch" statement to identify the number of rows you want to retrieve.
Instructions
-
-
1
Open the query editor for DB2 on the database server or a computer that has access to the server. Log in to the server using your username and password.
-
2
Right-click the procedure you want to edit or click "New" to create a new DB2 statement.
-
-
3
Create the query to retrieve records and limit the number of rows returned. The following code only returns the first 20 rows from the database table named "customers"
select * from customers fetch first 20 rows only
-
4
Click "Run" to execute the query. Notice in the results panel that only 20 rows return from your query.
-
1