× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On 15-May-2015 10:31 -0500, Mohan Eashver wrote:

I am writing a SQL script to be run interactively on STRSQL session.
The file has several thousand rows.
I like to retrieve let say only the first 100 rows.
What is the Syntax to use ?

I know that I was able to something like this in ORACLE world.
But ROWNUM is not recognized on IBM i (we are on V7R1)

SELECT * FROM ORDERS WHERE ROWNUM < 101


The Start Interactive SQL (STRSQL) by default, running output to display, will operate implicitly much like having specified the clause OPTIMIZE FOR 25 ROWS, so the effect may be similar, even without an explicit limitation for the number of row; i.e. generally [notably, if "ORDERS" is a TABLE] the control is returned to the report writer after the query obtains that many rows, and only enough rows are obtained by FETCH to present at the display, so the user sees only as many as they wish to /scroll/ through -- but of course that could be beyond 100.

However the ROWNUM concept generally implies an ORDER BY, of which the OP did not specify; without an ORDER BY, which of the rows are the /first 100/ is undefined and thus unpredictable.

If collation is irrelevant then just add the FETCH FIRST 100 ROWS ONLY clause to the end of that SELECT statement, and remove the WHERE clause:

    SELECT *
    FROM ORDERS
    FETCH FIRST 100 ROWS ONLY

More likely the desired effect is to obtain the First 100 rows according to a particular collation. For that, use the OLAP specifications <http://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/sqlp/rbafyolap.htm>, but AFaIK that necessarily will make the desired query much more complicated, unless the inclusion of the value for the ROWNUM in the result set is acceptable, despite that is not what the ORACLE query effects; e.g. one of the following:

    -- again using FETCH FIRST instead of less-than predicate
    -- notice the ROWNUM column is now part of the result set
    SELECT
      ROW_NUMBER() OVER(ORDER BY Some_Key) as ROWNUM
    , T1.*
    FROM ORDERS as T1
    FETCH FIRST 100 ROWS ONLY

Note: Remove the "ORDER BY Some_Key" and order is unspecified; that of course accomplishes nothing different than the earlier query, except adding the [possibly unwanted] ROWNUM values to the result set.

    -- using less-than predicate instead of FETCH FIRST; without
    -- the FETCH FIRST, the query likely performs much more work
    -- best IMO to keep the FETCH FIRST in the subquery and thus
    -- omit the then-redundant predicate in the WHERE clause
    SELECT N1.* /* or all columns named, except N1.ROWNUM */
    FROM ( SELECT
             ROW_NUMBER() OVER(ORDER BY Some_Key) as ROWNUM
           , T1.*
           FROM ORDERS as T1
           /* FETCH FIRST 100 ROWS ONLY */
         ) as N1
    WHERE ROWNUM < 101

Note: Obviously the column-list for the above query encapsulated in a VIEW is often ideal as contrasted with composing a complete column-list in ad-hoc queries.

    -- using less-than predicate instead of FETCH FIRST
    -- and join on primary key to omit the ROWNUM values
    -- best IMO to keep the FETCH FIRST in the
    -- derived\Nested Table Expression (NTE) and again
    -- omit the then-redundant predicate in the WHERE clause
    SELECT F1.*
    FROM ORDERS as F1
    JOIN ( SELECT
             ROW_NUMBER() OVER(ORDER BY Some_Key) as ROWNUM
           , T1.Pri_Key
           FROM ORDERS as T1
           /* FETCH FIRST 100 ROWS ONLY */
         ) as N1
      ON F1.Pri_Key = N1.Pri_Key
    WHERE N1.ROWNUM < 101

    -- AFaIK there is no support for the following syntax which
    -- would eliminate the ROWNUM value from the result-set
    SELECT *
    FROM "ORDERS"
    WHERE ROW_NUMBER() OVER(ORDER BY Some_Column ASC) < 101
    FETCH FIRST 100 ROWS ONLY


After I finished composing my reply, I figured this must be a FAQ, so I did a simple web search:
<https://www.google.com/search?q=db2+equivalent+of+rownum+of+oracle>

  Among other links, that led me to a link that directed me to:
<http://www.ibm.com/developerworks/data/library/techarticle/dm-0707rielau/>
DB2 Viper 2 compatibility features
Make your application ports easier
"...
_Limit result sets_
Sometimes it is necessary to limit the number of rows returned by a query. Oracle applications typically utilize the ROWNUM pseudo column for that purpose. In DB2, there are two options to achieve the same effect:

    FETCH FIRST n ROWS clause
    ROW_NUMBER() OLAP function
..."

But what the article covers is, of course, not about the DB2 for i. But the snippet helps explain what the ORACLE ROWNUM is [a pseudo-column] and what standard DB2 offers in-kind to simulate; the capability of the DB2 variant on IBM i getting such a "compatibility feature" [partly as an extension with allowing the ROW_NUMBER() in a WHERE clause] as something possible in the future -- as something which I am confident I have not seen noted yet anywhere as available, not in any docs or developerWorks discussions that I have read.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.