开发者

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

I 开发者_Python百科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?


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;


Missing the Only Keyword at the end. Example here.


While the example you give is likely simplified, how about putting the fetch first clause in the first select portion?

I can never read the documentation clearly, but as the with-statement creates a common-table-expression, you might not be able to use the fetch-first-clause on selecting from it. According to this documentation, having the fetch-first-clause in the select of the with-statement is valid syntax.

i.e.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜