在jasper ireport中传递空字段作为参数

时间:2013-05-09 11:15:16

标签: java sql sql-server jasper-reports

查询

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'为空时,查询应该拉出DB中的整个行。 如何修改查询以使其接受'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>

2 个答案:

答案 0 :(得分:1)

仅当我在jasper iReport中设计报表时才会出现此问题。但是一旦我在服务器中部署它,问题就解决了。服务器接受空值。

答案 1 :(得分: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的参数,类型为text
 ($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(因此,不要按日期约束)但如果参数具有值,请使用这些值。如果您的用户输入一个日期而不是另一个日期怎么办?在这种情况下,可能会将&amp;&amp; / AND更改为|| / OR并返回1 = 1。