在H2上运行查询时,找不到列“name_c”错误

时间:2013-02-07 17:03:05

标签: java h2 in-memory-database

我正在使用H2的内存实例进行单元测试,我正在尝试运行以下查询:

SELECT name AS name_c FROM users ORDER BY lower(name_c)

(真正的查询要复杂得多,但这个简单的查询具有相同的症状)

我收到以下例外:

org.h2.jdbc.JdbcSQLException: Column "NAME_C" not found; SQL statement:
SELECT name AS "name_c" FROM users ORDER BY lower(name_c) [42122-163]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.get(DbException.java:146)
    at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:138)
    at org.h2.expression.Function.optimize(Function.java:1705)
    at org.h2.command.dml.Select.prepare(Select.java:799)
    at org.h2.command.Parser.prepareCommand(Parser.java:218)
    at org.h2.engine.Session.prepareLocal(Session.java:415)
    at org.h2.engine.Session.prepareCommand(Session.java:364)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1121)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:164)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:152)
    at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
    at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
    at com.name.dal.Connection.executeQuery(Connection.java:462)
    at com.name.model.users.UserTest.testTest(UserTest.java:324)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

任何人都知道为什么?

我正在使用以下连接字符串创建数据库:

jdbc:h2:mem:db1;MODE=MYSQL;DB_CLOSE_DELAY=60;IGNORECASE=TRUE

谢谢,

1 个答案:

答案 0 :(得分:1)

列别名仅在order by子句中可用(如果它们按原样使用)(单独使用)。这与其他数据库的行为相同(根据我的测试,PostgreSQL和Derby)。有些数据库确实支持你想要的东西(例如MySQL),但这不是SQL标准的一部分。例如:

create table test(id int primary key, name varchar(255));
insert into test values(1, 'hello');
insert into test values(2, 'world');

这适用于我测试的所有数据库:

select name as c_name from test order by c_name;
select name as c_name from test order by lower(name);

使用这些数据,对于PostgreSQL,Apache Derby和H2,以下查询失败了。它适用于MySQL:

select name as c_name from test order by lower(c_name);