HSQLDB - 可更新的语句不能与" SELECT TOP"或" ORDER BY"

时间:2016-02-02 21:13:10

标签: hsqldb

我使用HSQLDB和preparedStatements就好了,但如果我包含" SELECT TOP"或" ORDER BY"在我的SQL语句中,当我调用updateBoolean(或UpdateInt等)时,我遇到了异常:

java.sql.SQLException: attempt to assign to non-updatable column

此示例代码工作正常:

preparedStatement = connection.prepareUpdatable(
    "SELECT " + MyTable.COL_ID + ", " +
        MyTable.COL_READ +
        " FROM " + MyTable.NAME +
        " WHERE " + MyTable.COL_LOCAL +
        " =? AND " + MyTable.COL_REMOTE +
        " =?",
    ResultSet.TYPE_FORWARD_ONLY,
    ResultSet.CONCUR_UPDATABLE);

preparedStatement.setString(1, localAddress);
preparedStatement.setString(2, remoteAddress);

ResultSet rs = connection.query(preparedStatement);

if (rs.next())
{
    rs.updateBoolean(MyTable.COL_READ, isRead);
    rs.updateRow();
}

如果我改变" SELECT"我会得到例外。到"选择TOP"。或者,如果我将其附加到SQL语句:

" ORDER BY " + MyTable.COL_RECEIVED_TIMESTAMP + " DESC"

感谢您的帮助。

NickB

1 个答案:

答案 0 :(得分:2)

可更新的SELECT语句不能包含TOP nLIMITORDER BY。这个限制是由SQL标准强加的。当您添加其中一个关键字时,您的SELECT将无法更新。

可以在带有上述关键字的WITH子句中使用子查询,并且SELECT是可更新的。

CREATE TABLE t (a int, b int, PRIMARY KEY(a));

WITH SUBQ(COL) AS (SELECT TOP 1 a FROM t) 
  SELECT * FROM t WHERE a IN (SELECT * FROM SUBQ)