SQL-在DAO中执行时,列名无效

时间:2019-02-02 09:50:45

标签: java spring oracle plsql spring-jdbc

我正在对Spring Application进行查询,该查询将基于此查询选择数据库中的数据:

SELECT * FROM
    (
        SELECT a.*, rownum r__
        FROM
        (
            SELECT * FROM SUBSCRIPTIONS WHERE status = 'active' and is_blocked = 'N'
        ) a
        WHERE rownum < ((1 * 3) + 1 )
    )
WHERE r__ >= (((1-1) * pageSize) + 1)

目标是仅对结果进行分页。

当我在数据库查询窗口中执行代码时,得到了预期的结果,但是当直接在Java代码中执行时,通常会出现此错误:

  

原因:org.springframework.jdbc.BadSqlGrammarException:   PreparedStatementCallback;错误的SQL语法[SELECT * FROM(SELECT   a。*,rownum r__ FROM(SELECT * FROM SUBSCRIPTIONS   WHERE status ='active'和is_blocked ='N')WHERE rownum <   ((?*?)+ 1))在哪里r__> =(((?-1)*?)+ 1)]];嵌套异常为   java.sql.SQLException:无效的列名

我不明白为什么会返回此错误,因为我的查询在Oracle SQL Client上执行时有效...

要执行我的查询,请执行以下操作:

MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("pageSize", paging.getItemsPerPage());
        params.addValue("pageNumber", paging.getPageToFetch());
        SqlQueryLogger.logDebugSqlQuery(selectActiveAndUnblockedSubscriptionPaged, params, log);

        List<Subscription> subscriptions = getNamedParameterJdbcTemplate().query(selectActiveAndUnblockedSubscriptionPaged, params, new SubscriptionRowMapper());
        SubscriptionPagingResult subscriptionPagingResult = new SubscriptionPagingResult();
        subscriptionPagingResult.setResult(subscriptions);

和selectActiveAndUnblockedSubscriptionPaged变量是:

"SELECT * FROM" +
            "(" +
            "    SELECT a.*, rownum r__" +
            "    FROM" +
            "    (" +
            "        SELECT * FROM SUBSCRIPTIONS WHERE status = 'active' and is_blocked = 'N'" +
            "    ) a" +
            "    WHERE rownum < ((:pageNumber * :pageSize) + 1 )" +
            ")" +
            "WHERE r__ >= (((:pageNumber-1) * :pageSize) + 1)";

有人知道为什么此代码不起作用吗? 谢谢

1 个答案:

答案 0 :(得分:1)

只是一个建议,您在()的thr main之后错过了表别名

SELECT t.* FROM
(
    SELECT a.*, a.rownum r__
    FROM
    (
        SELECT * 
        FROM SUBSCRIPTIONS 
        WHERE status = 'active' 
        and is_blocked = 'N'
    ) a
    WHERE a.rownum < ((1 * 3) + 1 )
) t 
WHERE t.r__ >= (((1-1) * t.pageSize) + 1)