Why does FETCH FIRST N ROWS not work in combination with WITH statement?

Boris picture Boris · Sep 27, 2011 · Viewed 15.9k times · Source

I have the following SQL statement which does not run on my DB2 database:

WITH a AS (
    SELECT * FROM sysibm.systables
)
SELECT a.* FROM a
FETCH FIRST 10 ROWS

Without the FETCH statement it works. The error message I get is:

Illegal use of keyword OPTIMIZE, token ERR_STMT WNG_STMT GET SQL SAVEPOINT HOLD FREE ASSOCIATE was expected.

Any suggestions?

Answer

Joe Stefanelli picture Joe Stefanelli · Sep 27, 2011

You're missing the ONLY keyword at the end of the FETCH clause.

WITH a AS (
    SELECT * FROM sysibm.systables
)
SELECT a.* FROM a
FETCH FIRST 10 ROWS ONLY;