Reputation: 11
Is there any way to add parameters conditionally in iReport?
In one of my report I want to pass parameter based on its value. Following is report query:
select name,desi,class from student where class=$P{calss} AND joinYear=$P{joinYear}
I want to add joinYear condition only if joinYear is passed, if value of joinYear is blank then only one condition should apply.
Is there any way to check condition in report query in iReport?
Upvotes: 1
Views: 5174
Reputation: 164
create a new parameter joinYear_query and set to DEFAULT VALUE EXPRESSION
$P{joinYear}==null ? " true": " joinYear=".concat($P{joinYear}.toString())
then
select name,desi,class from student where class=$!P{calss} AND $P!{joinYear_query}
you can see more details in : Accepting null values as parameters in jasper report
Upvotes: 0
Reputation: 522
You could try with a subreport. Create a dummy main report, which will call your report as a subreport, with a dummy query like
select 1 as dummy
and add a variable ie. $V{joinYearCond}
with expression
($P{joinYear} == "" || $P{joinYear} == null) ? "" : "and joinYear = " + $P{joinYear}
Also modify the query in your report (now a subReport)
select name,desi,class from student where class=$P{calss} $P!{joinYearCond}
For parameter $P{joinYearCond}
pass the value of $V{joinYearCond}
. It doesn't look pretty but it will work.
Upvotes: 1
Reputation: 3548
You can try this :-
SELECT name,
desi,
class
FROM student
WHERE (class=$P{calss} or $P{calss} is null )
AND (joinYear=$P{joinYear} or $P{joinYear} is null)
In this case whatever parameter you will pass only that parameter value will pass in the query.
Upvotes: 3