我在iReport的报告查询向导中有以下查询
select docid_fname_pemid.*, MONTHNAME(b.ServicePeriodDate) as month_name,YEAR(b.ServicePeriodDate) as year_name , b.NonLTCMaximumSpecialPayment, b.NonLTCEnrolledPatientOutsideUseTotal, b.NonLTCAccessBonus
from (
select docid_pemid.PEMID, docid_pemid.DoctorID, b.$P{transparency_check})
from (
select DoctorID,PEMID from DoctorPEMMap where PEMID in ($P{PEMID_input}) and StartDate >= $P{StartDate} and (EndDate <= $P{EndDate} or EndDate <= '0000-00-00') group by PEMID order by PEMID
)docid_pemid left join Doctors b on docid_pemid.DoctorID=b.DoctorID
) docid_fname_pemid
left join DoctorPayments b on docid_fname_pemid.DoctorID=b.DoctorID
&安培;参数,按顺序排列(参数类,提示是/否,默认值表达式)
1)PEMID_input
- &GT;字符串,提示是,否
2)month_year
- GT; .String,提示是,否
3)transparency_input
- &GT;字符串,提示是,否
4)transparency_check
- &gt;字符串,无提示,($P{transparency_input}=="yes" ) ? ("FirstName") : ("AliasFirstName")
5)StartDate
- &gt;字符串,无提示,$P{month_year}.split("-")[0]=="April" ? $P{month_year}.split("-")[1].concat("-04-01") : $P{month_year}.split("-")[1].concat("-10-01")
6)EndDate
- &gt;字符串,无提示,$P{month_year}.split("-")[0]=="April" ? $P{month_year}.split("-")[1].concat("-09-30") : $P{month_year}.split("-")[1].concat("-03-31")
当我运行报告时,给出以下错误
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near ''FirstName'
from ( select DoctorID,PEMID from DoctorPEMMap where PEMID in ('21' at line 3
在
我认为查询未获得'b.FirstName'
。所以我使用concat函数作为concat('b.',$P{transparency_check})
,但它不起作用。
我想在mysql查询中最终b.FirstName
或b.AliasFirstName
。当我手动提供这些术语时,查询运行正常。
我该怎么办?
答案 0 :(得分:2)
您遇到的问题与Jasper插入参数值的方式有关。如果你只使用$ P {PARAMETER NAME},那么我相信它在编译SQL之后会填充参数。如果使用$ P!{PARAMETER NAME},则该参数将被视为文字,并在编译SQL之前填充。这就是为什么当你只使用$ P时,Jasper似乎在参数值周围插入单引号。
所以尝试改变这个:
b.$P{transparency_check}
对此:
b.$P!{transparency_check}
并在transparency_check之后删除额外的括号。
检查此链接。我认为它比我能解释得更好。
http://community.jaspersoft.com/wiki/using-report-parameters
以下是整个代码的外观。我格式化它使我更容易阅读。
SELECT docid_fname_pemid.*,
MONTHNAME(b.ServicePeriodDate) as month_name,
YEAR(b.ServicePeriodDate) as year_name ,
b.NonLTCMaximumSpecialPayment,
b.NonLTCEnrolledPatientOutsideUseTotal,
b.NonLTCAccessBonus
FROM
(
SELECT docid_pemid.PEMID, docid_pemid.DoctorID, b.$P!{transparency_check}
FROM
(
SELECT DoctorID,
PEMID
FROM DoctorPEMMap
WHERE PEMID IN ($P{PEMID_input})
AND StartDate >= $P{StartDate}
AND (EndDate <= $P{EndDate} or EndDate <= '0000-00-00')
GROUP BY PEMID
ORDER BY PEMID
) docid_pemid
left join Doctors b on docid_pemid.DoctorID=b.DoctorID
) docid_fname_pemid
left join DoctorPayments b on docid_fname_pemid.DoctorID=b.DoctorID