DB2 SQL Query to get row number for project

DB2 Tutorial
DB2 Tutorial

In DB2, after fetching rows from a table by using cursor, in result table you cannot see row numbers. If there is a requirement in your project you need to see row numbers in the result, then, the below Query is highly useful.

You can get row number by using below SQL query.

In Declare cursor, in SELECT statement give SQL query as follows:

Select row_number over(group by col_1, col_2, col_3)

from table SAMPLE;

In the result table row numbers appears.

In COBOL,DB2 program you cannot see row numbers. This is possible, if you give a query in SPUFI, and you will get row numbers for each row.


Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.