Subject Re: [firebird-support] Query returning row when it shouldn't...
Author Helen Borrie
At 03:54 PM 31/05/2004 +1200, you wrote:
>Hi, I'm a new Firebird user, using Firebird-1.5.0.4306-Win32 and
>Firebird-JDBC-SQL-1.5.0Beta3JDK_1.4.
>
>I have a new database , no data in it yet. When my application starts it
>looks for a value in the database, returning it if found, i.e something
>like:
>
> ...
> java.sql.Statement s = myConnection.createStatement();
> ResultSet rs = s.executeQuery("select max(PK_SN) from MYTABLE");
> if (rs.next())
> // Then the row exists - return the value:
> return rs.getInt("PK_SN");
>
>However an exception occurs on the last line:
>"org.firebirdsql.jdbc.FBSQLException: Column name PK_SN not found in
>result set"
>This in itself is not a suprise because there are no rows... the problem
>is that rs.next() should return false (indicating that there is not a
>'next' row).
>
>Can anyone confirm that this is a bug and suggest the 'firebird' way of
>doing it? (This sort of thing works properly with SQLserver )

Ahem, the output of an expression is *never* the column name of the column
that was operated on. Last time I used SQL Server it wasn't the case
there, either.

Recode this as:

...
java.sql.Statement s = myConnection.createStatement();
ResultSet rs = s.executeQuery("select max(PK_SN) as RESULT from MYTABLE");
if (rs.next())
// Then the row exists - return the value:
return rs.getInt("RESULT");

I can't make sense of the Subject of your message. SELECT <an expression>
should return a result. In this case, it will be NULL.

An even more life-saving piece of advice is: in a multi-transactional
system, never use this as a way to decide what's going to be the next
number in a series, ***especially*** if you're doing it to keys.

/heLen