请有人告诉我,即使我在同一个事务下运行两个查询,为什么我的sql会得到“无效对象#EMP_TEMP”的异常呢?
@Transactional
public Map<String, EventType> findEventsByDateRange(final Date startTimestamp, final Date endTimestamp) throws Exception {
log.debug("Fetching Events Data");
String EVENT_QUERY = "Select ID, Name, Status, JoinDate into #EMP_TEMP from EMPLOYEE where JoinDate >= ? and JoinDate < ?";
this.jt.execute(EVENT_QUERY, new PreparedStatementCallback<Boolean>() {
@Override
public Boolean doInPreparedStatement(PreparedStatement preparedStatement) throws SQLException, DataAccessException {
preparedStatement.setTimestamp(1, new java.sql.Timestamp(startTimestamp.getTime()));
preparedStatement.setTimestamp(2, new java.sql.Timestamp(endTimestamp.getTime()));
return preparedStatement.execute();
}
});
//this.jt.execute(EVENT_QUERY);
return this.jt.query("SELECT * from #EMP_TEMP "
, DataExtractor.eventDataExtractor);
}
但是,如果我按下面的方式更改代码,它就不会抱怨。但是,这种方法的问题是我不能将任何参数传递到第一个查询中:
@Transactional
public Map<String, EventType> findEventsByDateRange(final Date startTimestamp, final Date endTimestamp) throws Exception {
log.debug("Fetching Events Data");
String EVENT_QUERY = "Select ID, Name, Status, JoinDate into #EMP_TEMP from EMPLOYEE where JoinDate >= '2015-07-13 00:00:00.000' and JoinDate < '2015-07-14 00:00:00.000'";
/*this.jt.execute(EVENT_QUERY, new PreparedStatementCallback<Boolean>() {
@Override
public Boolean doInPreparedStatement(PreparedStatement preparedStatement) throws SQLException, DataAccessException {
preparedStatement.setTimestamp(1, new java.sql.Timestamp(startTimestamp.getTime()));
preparedStatement.setTimestamp(2, new java.sql.Timestamp(endTimestamp.getTime()));
return preparedStatement.execute();
}
});*/
this.jt.execute(EVENT_QUERY);
return this.jt.query("SELECT * from #EMP_TEMP "
, DataExtractor.eventDataExtractor);
}
发布于 2015-07-21 05:55:58
最后,我发现问题的根本原因不是spring,而是sql server。
在Server 2005、SQL Server 2000和SQL Server 7.0中,无法使用准备好的语句创建临时对象,也不能引用创建临时对象(例如临时表)的系统存储过程。这些程序必须直接执行。
由于我试图通过jdbcTemplate方法创建临时表,所以执行(String,PreparedStatementCallback操作)使用了准备好的语句,因此无法工作。
相反,当我使用execute(String )创建临时表时,它正在工作。
https://stackoverflow.com/questions/31463783
复制相似问题