1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nil
  • 11
  • 1
  • 2
  • possible duplicate of [How to give condition for null values of multi select parameter in Ireport?](http://stackoverflow.com/questions/9782107/how-to-give-condition-for-null-values-of-multi-select-parameter-in-ireport) & [Request report with parameters](http://stackoverflow.com/q/17663860/876298) & [Conditional Where clauses in JasperReports](http://stackoverflow.com/q/12324706/876298) & [optional where clause jasper reports](http://stackoverflow.com/q/19401825/876298) – Alex K Nov 22 '13 at 10:47

3 Answers3

3

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.

Sharad
  • 3,562
  • 6
  • 37
  • 59
1

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.

0

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

Angeldev
  • 164
  • 1
  • 4