在结果集中放置Union查询

时间:2011-12-12 02:26:18

标签: sql eclipse jsp jdbc

是否有人建议是否有任何规则在jdbc结果集中放置联合查询? 我在数据库中有一个联合查询,我得到了输出...但是当我试图在结果集中放置相同的Union查询时,

  

收到错误:ASA错误-131:'unionselect'附近的语法错误   在线...

任何人都可以建议我出错的地方......

jsp页面中的查询如下.....

String QueryString = 

"select CONVERT(VARCHAR(7),[startdatetime],111) AS [year-month],nm.nameLine1 as CompanyName, sum(datediff(hour, startdatetime, enddatetime)) as total" 
+" from servicerequestworklog as srl"
+" inner join ServiceRequest sr on srl.ServiceRequestId = sr.ServiceRequestId"
+" inner join Name nm on(sr.clientCustomerId = nm.customerId and nm.nameTypeId = 'OFIC')"
+" where (startdatetime >= '08-01-2011 00:00:00.000' and enddatetime <= '10-31-2011 00:00:00.000')"
+" group by CompanyName, [year-month]"
+"union" 
+"select CONVERT(VARCHAR(7),[startdatetime],111) AS [year-month], 'ZZTOTAL' as CompanyName, sum(datediff(hour, startdatetime, enddatetime)) as total"
+" from servicerequestworklog as srl"
+"inner join ServiceRequest sr on srl.ServiceRequestId = sr.ServiceRequestId"
+" where (startdatetime >= '08-01-2011 00:00:00.000' and enddatetime <= '10-31-2011 00:00:00.000')"
+" group by [year-month]"
+"order by CompanyName, [year-month]" ;

1 个答案:

答案 0 :(得分:1)

您可以使用PreparedStatement(参数化查询)来避免这些类型的错误。我认为您必须在unionselect字之间添加空格。