问题
I want to create a report with JasperReports with multiple parameters, the report is generated correctly when the user passes all the parameters but nothing is generated when one parameter is missed i use this request
SELECT
t.*,
u."name" AS username,
c."name" AS componentName,
s."designation" AS statusName,
pr."name" AS priorityName,
p."name" AS projectName
FROM
"component" c INNER JOIN "ticket" t ON c."id" = t."component_id"
INNER JOIN "personne" u ON t."personne_id" = u."id"
INNER JOIN "status" s ON t."status_id" = s."id"
INNER JOIN "priority" pr ON t."priority_id" = pr."id"
INNER JOIN "project" p ON c."project_id" = p."id"
WHERE
pr.name = $P{priority}
and u.login = $P{userLogin}
and s.designation = $P{status}
and t.creation_date between $P{start} and $P{end}
and c.name = $P{componenet}
Please can you help me to generate the report even where there is one parameter missed?
回答1:
do you want outer joins? or you may choose a structure like this:
and ( u.login = $P{userLogin} or $P{userLogin} is null )
for each line
回答2:
You can set the default values for every parameter or use this query:-
SELECT
t.*,
u."name" AS username,
c."name" AS componentName,
s."designation" AS statusName,
pr."name" AS priorityName,
p."name" AS projectName
FROM
"component" c INNER JOIN "ticket" t ON c."id" = t."component_id"
INNER JOIN "personne" u ON t."personne_id" = u."id"
INNER JOIN "status" s ON t."status_id" = s."id"
INNER JOIN "priority" pr ON t."priority_id" = pr."id"
INNER JOIN "project" p ON c."project_id" = p."id"
WHERE
(pr.name = $P{priority} or $P{priority} is null)
and (u.login = $P{userLogin} or $P{userLogin} is null)
and (s.designation = $P{status} or $P{status} is null )
and t.creation_date between $P{start} and $P{end}
and (c.name = $P{componenet} or $P{componenet} is null)
回答3:
You can control the where clause in java class before generate the report.
1.In java u can check whether the parameter is null or not, then do small validation such as
StringBuffer sb = new StringBuffer();
if(StringUtils.isNotEmpty(priority)){
sb.append(" AND pr.name = "+priority);
}
2.do for every other condition for u.login, s.designation
3.after that u can pass reportParam.put("sqlQuery", sb.toString());
4.the in ireport simply change your query
WHERE
pr.name = $P{priority}
and u.login = $P{userLogin}
and s.designation = $P{status}
and t.creation_date between $P{start} and $P{end}
and c.name = $P{componenet}
to
WHERE 1=1 $P!{sqlQuery}
now you dont need to worry about the null
, since it will ignore the condition in java class.
来源:https://stackoverflow.com/questions/17663860/request-report-with-parameters