1

我在一个表中有 n 列。我希望有一个选择标准,就好像一个值column2是空的,然后选择基于的行column 1,如果column2不是空的,那么基于的值选择column 2。我有一个很长的查询,在引入CASE WHEN THEN之前运行良好,但是当我在查询中添加案例时,我得到ORA-00905 。示例查询如下:

select * from table where table.testcolumn='some value' and ( case when table.column2 is NULL then  table.column1 = 'value' else table.column2 = 'value' end )

例外:

Caused by: java.sql.SQLSyntaxErrorException: ORA-00905: missing keyword

    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:91)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1035)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:194)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:791)
    at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:866)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1187)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3386)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3430)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
    at weblogic.jdbc.wrapper.PreparedStatement.executeQuery(PreparedStatement.java:128)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
    at org.hibernate.loader.Loader.doQuery(Loader.java:674)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2213)
4

1 回答 1

1

我认为您可以使用替代语句而不使用 case when..:

select * from table 
where table.testcolumn='some value' 
and ( 
      (table.column2 is NULL and table.column1 = 'value') 
      or (table.column2 = 'value')
    )
于 2017-05-18T16:45:36.747 回答