1

我在一个函数中执行以下查询,但出现“SQLGrammarException:无法执行本机批量操作查询错误”异常。

任何人都可以告诉我解决方案吗?我不想使用 HQL 。

    query = session.createSQLQuery("CREATE TEMPORARY TABLE temp select `"+xAxis+"`, sum(value) as length from (select * from "+xAxis+"Table union select `"+xAxis+"`, sum(cycles) as value from "+Constants.TABLE_NAME+" where datestamp >= '"+startDate+"'  and datestamp < '"+endDate+"'  and `"+groupBy+"` = '"+groupByBar1+"' and `"+xAxis+"` = 'Uptime' group by `"+xAxis+"`) as t group by `"+xAxis+"` "+fixOrderString+" ;CREATE TEMPORARY TABLE test select `"+xAxis+"`, sum(value) as length from (select * from "+xAxis+"Table union select `"+xAxis+"`, sum(cycles) as value from "+Constants.TABLE_NAME+" where datestamp >= '"+startDate+"'  and datestamp < '"+endDate+"' and  `"+xAxis+"` = 'Uptime' group by `"+xAxis+"`) as t group by `"+xAxis+"` "+fixOrderString+" ;alter table test add column `"+groupByBar1+"` INTEGER ;");  
            query.executeUpdate();              
            query = session.createSQLQuery("update test,temp set `"+groupByBar1+"` = temp.length where test.`"+xAxis+"` = temp.`"+xAxis+"` ; drop table temp ; CREATE TEMPORARY TABLE temp select `"+xAxis+"`, sum(value) as length from (select * from "+xAxis+"Table union select `"+xAxis+"`, sum(cycles) as value from "+Constants.TABLE_NAME+" where datestamp >= '"+startDate+"'  and datestamp < '"+endDate+"' and  `"+groupBy+"` = '"+groupByBar2+"' and `"+xAxis+"` = 'Uptime' group by `"+xAxis+"`) as t group by `"+xAxis+"` "+fixOrderString+"  ;alter table test add column `"+groupByBar2+"` INTEGER ; ");
            query.executeUpdate();
            query = session.createSQLQuery("update test,temp set `"+groupByBar2+"` = temp.length where test.`"+xAxis+"` = temp.`"+xAxis+"` ; drop table temp ; CREATE TEMPORARY TABLE temp select `"+xAxis+"`, sum(value) as length from (select * from "+xAxis+"Table union select `"+xAxis+"`, sum(cycles) as value from "+Constants.TABLE_NAME+" where datestamp >= '"+startDate+"'  and datestamp < '"+endDate+"' and  `"+groupBy+"` = '"+groupByBar3+"' and `"+xAxis+"` = 'Uptime' group by `"+xAxis+"`) as t group by `"+xAxis+"` "+fixOrderString+"  ;alter table test add column `"+groupByBar3+"` INTEGER ;");
            query.executeUpdate();
            query = session.createSQLQuery("update test,temp set `"+groupByBar3+"` = temp.length where test.`"+xAxis+"` = temp.`"+xAxis+"` ; drop table temp ; CREATE TEMPORARY TABLE temp select `"+xAxis+"`, sum(value) as length from (select * from "+xAxis+"Table union select `"+xAxis+"`, sum(cycles) as value from "+Constants.TABLE_NAME+" where datestamp >= '"+startDate+"'  and datestamp < '"+endDate+"' and  `"+groupBy+"` = '"+groupByBar4+"' and `"+xAxis+"` = 'Uptime' group by `"+xAxis+"`) as t group by `"+xAxis+"` "+fixOrderString+"  ;alter table test add column `"+groupByBar4+"` INTEGER ;");
            query.executeUpdate();
            query = session.createSQLQuery("update test,temp set `"+groupByBar4+"` = temp.length where test.`"+xAxis+"` = temp.`"+xAxis+"` ; drop table temp ; CREATE TEMPORARY TABLE temp select `"+xAxis+"`, sum(value) as length from (select * from "+xAxis+"Table union select `"+xAxis+"`, sum(cycles) as value from "+Constants.TABLE_NAME+" where datestamp >= '"+startDate+"'  and datestamp < '"+endDate+"' and  `"+groupBy+"` = `"+Constants.OTHERS+"' and `"+xAxis+"` = 'Uptime' group by `"+xAxis+"`) as t group by `"+xAxis+"` "+fixOrderString+"  ;alter table test add column `"+Constants.OTHERS+"' INTEGER ;");
            query.executeUpdate();
            query = session.createSQLQuery("update test,temp set `"+Constants.OTHERS+"` = temp.length where test.`"+xAxis+"` = temp.`"+xAxis+"`;");
            query.executeUpdate();
            query = session.createSQLQuery("select * from test "+fixOrderString+" ;");
4

1 回答 1

0

您想捕获该异常并将其打印出来以查看根本原因。例如,您可以使用 Log4j 执行此操作:

log.error("Error", e);

根本原因可能是由于某种原因,在第一个语句中创建的临时表实际上没有在以下语句中看到。

于 2012-12-10T22:53:45.947 回答