1

查询

select * from db_accessadmin.customerSummary 
where 
(accountNumber = $P{accountNo} or $P{accountNo}='')
and (ppuserMobile = $P{mobileNo} or $P{mobileNo}='')
and ( ppuserStaticID = $P{customerId} or $P{customerId} = '')
and (cast(requestDate as date) between (cast($P{fromDate} as date)) and (cast($P{toDate} as date)))

目的是根据参数的值在 jasper 中生成报告。当参数 'fromDate' 和 'toDate' 为空时,查询应该拉出数据库中的整个行。如何修改查询,使其接受“fromDate”和“toDate”的空值。

XML 文件

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="customerSummary2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d23efc9c-641e-4e8a-bb9e-25673ee5c713">
<property name="ireport.zoom" value="1.610510000000001"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="accountNo" class="java.lang.String"/>
<parameter name="mobileNo" class="java.lang.String"/>
<parameter name="customerId" class="java.lang.String"/>
<parameter name="fromDate" class="java.util.Date">
    <defaultValueExpression><![CDATA[new Date()]]></defaultValueExpression>
</parameter>
<parameter name="toDate" class="java.util.Date">
    <defaultValueExpression><![CDATA[$P{fromDate}+7]]></defaultValueExpression>
</parameter>
<queryString>
    <![CDATA[select * from db_accessadmin.customerSummary where (accountNumber =         $P{accountNo} or $P{accountNo}='')
 and (ppuserMobile = $P{mobileNo} or $P{mobileNo}='')
 and ( ppuserStaticID = $P{customerId} or $P{customerId} = '')
 and (cast(requestDate as date) between (cast($P{fromDate} as date)) and (cast($P{toDate} as date)))]]>
</queryString>
4

2 回答 2

1

This issue only occurs when i design the report in jasper iReport. But once i deploy it in the server, the issue is resolved. The server accepts null values.

于 2013-05-14T07:19:04.350 回答
0

将您的查询更改为:

 select * from db_accessadmin.customerSummary 
 where 
 (accountNumber = $P{accountNo} or $P{accountNo}='')
 and (ppuserMobile = $P{mobileNo} or $P{mobileNo}='')
 and ( ppuserStaticID = $P{customerId} or $P{customerId} = '')
 $P!{date_sql}

这是我在 iReport 中使用 Groovy 的方法,因此您必须针对 Java 进行修改。使用默认值表达式创建另一个名为 date_sql 的文本类型参数:

 ($P{fromDate}.isEmpty() || $P{fromDate} == null) 
 && ($P{toDate}.isEmpty() || $P{toDate} == null) 
 ? "and 1=1" : "and (cast(requestDate as date) between (cast("+$P{fromDate}+" as date)) and (cast("+$P{toDate}+" as date)))"

这是未经测试的,因此您可能不得不使用默认值表达式的第一部分。但本质上,当参数为空或 null 时,返回 1=1(因此,不要受日期约束),但如果参数有值,则使用这些值。如果您的用户输入一个日期而不输入另一个日期怎么办?在这种情况下,也许将 &&/AND 更改为 ||/OR 并返回 1=1。

于 2013-05-09T15:27:23.463 回答