I am trying this query but without success.
SELECT name, phone_office, billing_address_city, billing_address_street, billing_address_country
FROM accounts
WHERE ($P!{EmployeeID} is null or assigned_user_id = $P!{EmployeeID})
ORDER BY billing_address_country, billing_address_city
This url will filter by EmployeeID and works fine:
.../flow.html?_flowId=viewReportFlow&reportUnit=/reports/samples/EmployeeAccounts&EmployeeID=sarah_id
But when i remove the EmployeeID parameter i want to remove the filter where. So all results should be shown.
.../flow.html?_flowId=viewReportFlow&reportUnit=/reports/samples/EmployeeAccounts
My question is, what is the correct way of passing an optional where in sql query.
Ok, let see the sample.
For example we have a query:
But our inputParamCity can be undefined. In this case we got an error:
How we can fix it?
It is very simple - we can add another parameter with default expression like this:
-if the inputParamCity parameter is undefined the "fake" clause "1=1" will be used, in other case the filter by city field will be applied.
And of course we have to modify the query expression - to use this new parameter. Our query expression in this case will be:
The sample
The jrxml file:
In case using undefined inputParamCity parameter (the value is not set) the result will be:
In this case the query was used by engine is:
If we set, for example, the value Chicago for inputParamCity parameter the result will be:
In this case the query was used by engine is:
Notes:
You can find more information in this post: JasperReports: Passing parameters to query
You can modify the whereClause parameter's expression and the query expression. For example, you can move WHERE keyword from query expression to parameter's expression to prevent using the fake clause "1=1"
I think optional where clause can be done in this way also :-
In this condition if you do not pass any employee id it gives you all the values without applying where clause condition.