开发者

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

开发者 https://www.devze.com 2023-04-08 07:41 出处:网络
I 开发者_Python百科have the following SQL statement which does not run on my DB2 database: WITH a AS (

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

精彩评论

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

关注公众号