Fetching rows in DB2

前端 未结 2 552
旧巷少年郎
旧巷少年郎 2021-02-04 06:56

I know in DB2 (using version 9.7) I can select the first 10 rows of a table by using this query:

SELECT * 
FROM myTable
ORDER BY id
FETCH FIRST 10 ROWS ONLY


        
相关标签:
2条回答
  • 2021-02-04 07:29

    Here's a sample query that will get rows from a table contain state names, abbreviations, etc.

    SELECT *
    FROM (
       SELECT stabr, stname, ROW_NUMBER() OVER(ORDER BY stname) AS rownumber
       FROM states
       WHERE stcnab = 'US'
    ) AS xxx
    WHERE rownumber BETWEEN 11 AND 20 ORDER BY stname
    

    Edit: ORDER BY is necessary to guarantee that the row numbering is consistent between executions of the query.

    0 讨论(0)
  • 2021-02-04 07:44

    You can also use the MYSQL compatibility. You just need to activate the vector compatibility for MYS, and then use Limit and Offset in your queries.

    db2set DB2_COMPATIBILITY_VECTOR=MYS
    db2stop
    db2start
    
    • An excellent article written by DB2 experts from IBM https://www.ibm.com/developerworks/mydeveloperworks/blogs/SQLTips4DB2LUW/entry/limit_offset?lang=en
    • Compatibility vector in InfoCenter http://publib.boulder.ibm.com/infocenter/db2luw/v10r1/topic/com.ibm.db2.luw.apdv.porting.doc/doc/r0052867.html
    • A blog about this http://victorsergienko.com/db2-supports-limit-and-offset/
    0 讨论(0)
提交回复
热议问题