我在使用spring jdbcTemplate执行一个非常长的查询时遇到问题。该查询包含多个DECLARE
语句,多个CREATE
临时表,最后一个SELECT
语句通过连接多个表以及DROP
语句来获取数据。当我直接在SQL Management Studio中运行此查询时,查询运行完全正常并返回数据。但是,当我使用spring的jdbc模板运行完全相同的查询时,没有返回数据。
编辑:我无法发布我正在使用的确切查询。但下面是查询的外观模板。
public class SpringJdbcExample{
public static final String sql = "DECLARE "+
"@XYZ DATETIME='1/31/2016', " +
"@ABC UNIQUEIDENTIFIER='', " +
"@var1 VARCHAR (1)='F', " +
"@var2 UNIQUEIDENTIFIER = NULL " +
"DECLARE @id1 INT, @id2 INT, @id3 Int "+
"SELECT @id1=(SELECT id1 FROM table1 WHERE XYZ=@XYZ) "+
"SELECT @id2=(SELECT id2 FROM table2 WHERE ABC=@ABC) " +
"SET NOCOUNT ON "+
"SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED "+
"CREATE TABLE #temp1 (var1 INT, var2 INT, var2 INT, var4 varchar(20) ) "+
"CREATE TABLE #temp2 (var1 INT,var2 INT, var3 DATETIME, var4 varchar(20)) "+
-- More Create temp tables
-- Insert into temp tables from various DB tables
-- single Select statement from temp tables
-- Drop temp tables
"; // end of the string
private JdbcTemplate jdbcTemplate;
public SpringJdbcExample (JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
public List<String> getData(){
// MapSqlParameterSource
MapSqlParameterSource params = new MapSqlParameterSource("someParams", "someValues");
jdbcTemplate.query(sql,params);
}