Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes PostgreSQL supports "SELECT... FOR UPDATE OF..." SQL?
    primarykey
    data
    text
    <p>I am currently using "<strong>PostgreSQL 9.0.4</strong>" database with JDBC driver: "<strong>postgresql-9.0-801.jdbc4.jar</strong>".</p> <p>I have the following JDBC SQL using "<strong>SELECT... FOR UPDATE OF...</strong>" clause which retrieves a column value (long) and updates the value by incrementing in same transaction. <br/>I need to <strong>return the long value</strong>, thus I need to use SELECT Sql.</p> <pre><code>final PreparedStatement preparedStatement = _connection.prepareStatement( "select value from sequence where table_name='JOB' for update of value", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); long ret; try { final ResultSet resultSet = preparedStatement.executeQuery(); resultSet.next(); ret = resultSet.getLong(1); resultSet.updateLong(1, ret + 1); resultSet.updateRow(); } catch (SQLException ex) { ex.printStackTrace(); } finally { resultSet.close(); preparedStatement.close(); } return ret; </code></pre> <p>All other databases e.g. MySQL, Oracle, MS-SQL, Derby work fine with such SQL - but PostgreSQL always throws following exception.</p> <pre><code> org.postgresql.util.PSQLException: ERROR: relation "value" in FOR UPDATE/SHARE clause not found in FROM clause Position: 68 </code></pre> <p>I do have the table "<strong>sequence</strong>" with the "<strong>value</strong>" column properly created - so this may not be the infamous case-sensitivity issue.</p> <p>Does this mean Postgres does not support <strong>"SELECT ...FOR UPDATE OF.."</strong> SQL syntax? <br/>In that case what will be the best way to achieve the same effect (i.e. select and update with the same transaction)?</p> <p>Many thanks in advance,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload