SELECT * FROM (SELECT ? FROM DUAL) fails

753 views
Skip to first unread message

Boris Granveaud

unread,
Nov 17, 2016, 11:41:32 AM11/17/16
to H2 Database
Hello,

I have a problem with the following statement:

SELECT * FROM (SELECT ? FROM DUAL)

it gives this error with H2 1.4.191:

org.h2.jdbc.JdbcSQLException: Unknown data type: """?1"""; SQL statement:
SELECT * FROM (SELECT ? FROM DUAL) [50004-192]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.table.Table.setColumns(Table.java:425)
at org.h2.table.TableView.initColumnsAndTables(TableView.java:220)
at org.h2.table.TableView.init(TableView.java:98)
at org.h2.table.TableView.<init>(TableView.java:65)
at org.h2.table.TableView.createTempView(TableView.java:535)
at org.h2.command.Parser.readTableFilter(Parser.java:1168)

Code to reproduce:

PreparedStatement st = cnx.prepareStatement("SELECT * FROM (SELECT ? FROM DUAL)");
st.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
rs = st.executeQuery();
rs.next();
System.out.println(rs.getObject(1));
rs.close();
st.close();

"SELECT ? FROM DUAL" works fine.

The problems is that the type of the parameter cannot be known when the statement is prepared so I'm not sure how the parser should deal with that.

Boris.

Thomas Mueller Graf

unread,
Nov 29, 2016, 1:47:26 AM11/29/16
to h2-da...@googlegroups.com
Hi,

You could use "cast":

SELECT * FROM (SELECT CAST(? AS VARCHAR) FROM DUAL)
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to h2-database+unsubscribe@googlegroups.com.
To post to this group, send email to h2-da...@googlegroups.com.
Visit this group at https://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages