0

我正在尝试使用以下语句创建 JDBC 查询

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName +
            "where datediff(d,DATECOLUMN2,getdate()) <= 1";
st = conn1.createStatement();
rs = st.executeQuery(query);  //receiving error here

我收到以下错误消息

java.sql.SQLException: "d" is not a recognized table hints option. If it is intended as a parameter to a table-valued function or to the CHANGETABLE function, ensure that your database compatibility mode is set to 90.

我确定查询由于某种原因无法识别 datediff 函数我不知道为什么,因为我以前在同一个应用程序中使用 HQL 并检索值。

为了尝试使用我使用的替代功能

{fn TIMESTAMPADD( SQL_TSI_DAY, 1, CURRENT_TIMESTAMP)}

但它也失败了我后来发现这仅用于德比数据库

有人可以帮助我使用正确的 sql 函数来使用 JDBC 将日期与当前日期进行比较

4

4 回答 4

3
String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName "+
               "where datediff(day,DATECOLUMN2,getdate()) <= 1";
于 2013-01-10T03:17:25.487 回答
0

The posted snippet doesn't have a closing double quote between tableName and +, but I figure that is just a typo. However, in your real code, where precisely is the double quote? Is it directly after tablename, like this

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName" +

or after the space that follows tablename, like this

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName "+

It is very likely the former, because in that case the resulting query would look exactly the way as to cause the error you are getting. Take a look at this:

SELECT COLUMN1,DATECOLUMN2 FROM tableNamewhere datediff(d,DATECOLUMN2,getdate()) <= 1

You can see that where merges with the table name and datediff becomes an alias. What follows is interpreted as table hints. (You can specify table hints without WITH in older versions of SQL Server/older compatibility levels.) Consequently, SQL Server stumbles over d, as that is indeed an incorrect table hint.

于 2013-01-10T09:29:54.963 回答
0

“d”后面的逗号应该是一个点:

where datediff(d.DATECOLUMN2,getdate())
--------------- ^ dot here
于 2013-01-10T03:54:43.380 回答
0

from 之前有一个逗号。根据您对 SQL Server 运行此错误消息。

String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName " 
              +" where datediff(d,DATECOLUMN2,getdate()) <= 1";
于 2013-01-10T03:32:21.773 回答