Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2013
    Posts
    8

    Unanswered: Query returns row Number

    Hi all,

    I am using java with db2. When i execute the query it returns a Row number at 0th position

    Example
    Query:select first_name,last_name,age from sample

    IN TABLE
    first_name last_name age
    Chandara sekaran 25

    Excepted Result
    In 0th Position:Chandara
    In 1st Position:sekaran
    In 2nd Position:25

    Actual Result
    In 0th Position:1
    In 1st Position:Chandara
    In 2nd Position:sekaran
    In 3rd Position:25

    I want to know why the query return the row number
    When i execute in toad ,it is executing fine

    Could any one help me plz....

    Regards
    Chandarasekaran Myilsamy

  2. #2
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    The "row number" column is probably added by the Java framework you are using. Check its documentation.
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  3. #3
    Join Date
    Apr 2013
    Posts
    8
    Thanks aflorin , i will check . but some queries doesn't return the row number(Working fine).
    This leads the big confusion here


    Thanks
    Chandarasekaran Myilsamy

  4. #4
    Join Date
    Apr 2013
    Posts
    8
    Hi ,
    I found the root cause for Rownumber which is returned by the query.
    The DB2Dialect automatically append the Rownumber for Paging.(see the source code for DB2Dialect in getLimitString()).Is there any possibility to avoid the RowNumber in my resultset.

    Could you help me


    Regards
    Chandarasekaran Myilsamy

  5. #5
    Join Date
    Apr 2013
    Posts
    8
    Hi ,
    Here i posted the code Which is append the rownumber as a column In db2dialect



    public String getLimitString(String sql, boolean hasOffset) {

    int startOfSelect = sql.indexOf("select");

    StringBuffer pagingSelect = new StringBuffer( sql.length()+100 )
    .append( sql.substring(0, startOfSelect) ) //add the comment
    .append("select * from ( select ") //nest the main query in an outer select
    .append( getRowNumber(sql) ); //add the rownnumber bit into the outer query select list

    if ( hasDistinct(sql) ) {
    pagingSelect.append(" row_.* from ( ") //add another (inner) nested select
    .append( sql.substring(startOfSelect) ) //add the main query
    .append(" ) as row_"); //close off the inner nested select
    }
    else {
    pagingSelect.append( sql.substring( startOfSelect + 6 ) ); //add the main query
    }

    pagingSelect.append(" ) as temp_ where rownumber_ ");

    //add the restriction to the outer select
    if (hasOffset) {
    pagingSelect.append("between ?+1 and ?");
    }
    else {
    pagingSelect.append("<= ?");
    }

    return pagingSelect.toString();
    }

    private String getRowNumber(String sql) {
    StringBuffer rownumber = new StringBuffer(50)
    .append("rownumber() over(");

    int orderByIndex = sql.toLowerCase().indexOf("order by");

    if ( orderByIndex>0 && !hasDistinct(sql) ) {
    rownumber.append( sql.substring(orderByIndex) );
    }

    rownumber.append(") as rownumber_,");

    return rownumber.toString();
    }

    private static boolean hasDistinct(String sql) {
    return sql.indexOf("select distinct")>=0;
    }

  6. #6
    Join Date
    Mar 2003
    Posts
    280
    I don't understand. Why don't you just remove the part that adds rownumber?

  7. #7
    Join Date
    Apr 2013
    Posts
    8
    The RowNumber part is used for Pagination . If i remove that part ,pagination wont work

  8. #8
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    Your original question was:
    I want to know why the query return the row number
    When i execute in toad ,it is executing fine

    Now that you have the answer, can you tell us what is the problem?
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

  9. #9
    Join Date
    Mar 2003
    Posts
    280
    Quote Originally Posted by chandarasekaran View Post
    The RowNumber part is used for Pagination . If i remove that part ,pagination wont work
    You can keep it in the where clause, and remove it from the select part.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •