Writing multiple queries for single Jasper report based on condition check

时间:2015-08-07 01:32:49

标签: sql-server jasper-reports

I am trying to generate one Jasper report in that I need to write query based on start and end date parameters. Here start and end dates might be null values, so based on start and end dates my query should change.

So far I have taken 4 parameters like id, from, to, condition1, condition2 for condition1

I defined an expression like

$P{from} != null ? "and date >= $P{from}:""  
condition2 expression $P{to} != null ? "and date < $P{to}:""

And my query is like this

select name from app_info where id=$P{id} $P!{condition1} $P!{condition2}

This query is giving all records even if I give date limits. I checked with my database and this output is wrong.

1 个答案:

答案 0 :(得分:0)

“和日期&gt; = $ P {from}:”“不正确(检查引号)

表达式的结果字符串,如“and date&gt; =”+ $ P {from}:“”没有任何意义。

此Oracle数据库解决方案。 转换字符串到日期的表达式,例如:to_date('10 .04.2008 10:00','DD.MM.YYYY HH:mi')

<parameter name="from" class="java.util.Date"/>
<parameter name="to" class="java.util.Date"/>
<parameter name="condition1" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA[$P{from} != null ? "and myDateField >to_date('"+new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm").format($P{from})+"','DD.MM.YYYY HH:mi')":""]]></defaultValueExpression>
</parameter>
<parameter name="condition2" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA[$P{to} != null ? "and myDateField <=to_date('"+new  java.text.SimpleDateFormat("dd.MM.yyyy HH:mm").format($P{to})+"','DD.MM.YYYY HH:mi')":""]]></defaultValueExpression>
</parameter>

参数$ P {condition1}必须在$ P {from}之后声明。在这种情况下,参数顺序很重要(按值第一参数计算第二个参数)

HTH