[英]passing empty field as parameter in jasper ireport
查询
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)))
目的是根据参数的值以碧玉形式生成报告。 当参数'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>
仅当我在jasper iReport中设计报告时,才会出现此问题。 但是,一旦我将其部署到服务器中,该问题就解决了。 服务器接受空值。
将查询更改为:
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}
这是我将使用Groovy在iReport中进行的操作,因此您必须针对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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.