Jump to content

How to change SQL of subreport


edilmar

Recommended Posts

Hi,

 

I have Jasper + iReport 1.3.0.

Then, my report has a subreport with a SQL that I'd like to change dinamically. I have a dialog with many kinds of filters, some of them I use to change the SQL for Statement/ResultSet into main report, and some filters are to change SQL for subreport. But I don't know how to change SQL for this one dinamically.

Thanks for any help...

Link to comment
Share on other sites

  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

The problem is that the SQL may change some filter (AND) clausules. I have five filters, that users may use or not, and combine some of them. Then, the SQL may be:

 

Code:

select ... from ...
where filter1
and filter2

or

select ... from ...
where filter1
and filter2
and filter3

or

select ... from ...
where filter2
and filter4
and filter5

or

etc...

 

There is no need to use all filters togheter. When this occurs in main report, it is easy to solve, I only pass the ResultSet with any SQL code generated in Java code. But the problem is: I don't know how to pass a SQL (neither a ResultSet) to the subreport.

Link to comment
Share on other sites

I'm not 100% sure this is what you are looking for but it may help. Using a query like:

 

Select * from myTable

where field1=something

and field2=somethingelse

 

If what you want is to control the where statement and only test field1 sometimes, field2 sometimes and both fields some other time you can try an IF statement within the WHERE statement.

 

Set up a pair of parameters:

$P{Field1}

$P{Field2}

 

Select * from myTable

where

if (length($P{Field1}) = 0, true, if ($P{Field1} = "something", true, false))

and

if (length($P{Field2}) = 0, true, if ($P{Field2} = "somethingelse", true, false))

 

 

Basically, if the length of the field is zero true is returned so the field is not tested. If the length is greater then zero, the value is tested and the result of the test is used.

 

You can extend this a little further by passing another couple of params if a length of zero is something you want to test. Instead of testing for length = 0 you the another parameter say: $P{TestField1} = "Y" and $P{TestField2} = "Y" the sql statement would be:

 

$P{TestField1}

$P{TestField1}

$P{Field1}

$P{Field2}

 

Select * from myTable

where

if (length($P{TestField1}) = "Y", true, if ($P{Field1} = "something", true, false))

and

if (length($P{TestField2}) = "Y", true, if ($P{Field2} = "somethingelse", true, false))

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...