I know I can update a single record like this - but then how to I get access to the id of the record that was updated? (I'm using MSSQL so I can't use Oracles RowId)
update myTable
set myCol = 'foo'
where itemId in (select top 1 itemId from myTable )
If I was peforming an Insert I could use getGeneratedKeys to get the id field value, but I don't think there is an equivalent for an update?
I know I can use a scrollable resultset to do what I want
i.e.
stmt = conn.prepareStatement("select top 1 myCol, itemId from myTable", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet resultSet = stmt.executeQuery();
if(resultSet.first()){
resultSet.updateString(1, "foo");
resultSet.updateRow();
String theItemId = resultSet.getString(1)
}
resultSet.close();
but I'm concerned about performance as testing shows lock timeouts under load and I was wondering if there was a better/simpler way?
-- EDIT: Just to finalise this issue... When we migrate to MSSQL2005 we will upgrade our code to use Rich's answer. In the current release we have used the lock hints: (UPDLOCK ROWLOCK READPAST) to mitigate the performance problems our original code showed.