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)))

目的は、パラメーターの値に応じてジャスパーでレポートを生成することです。パラメータ 'fromDate' と 'toDate' が空の場合、クエリは DB 内の行全体を引き出す必要があります。「fromDate」と「toDate」の null 値を受け入れるようにクエリを変更するにはどうすればよいですか。

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

この問題は、jasper iReport でレポートを設計した場合にのみ発生します。しかし、サーバーにデプロイすると、問題は解決します。サーバーは null 値を受け入れます。

于 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}

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 を返します (したがって、日付による制約はありません) が、パラメーターに値がある場合は、それらの値を使用します。ユーザーが 1 つの日付を入力し、もう 1 つの日付を入力しない場合はどうなりますか? &&/AND を ||/OR に変更し、その場合は 1=1 を返します。

于 2013-05-09T15:27:23.463 に答える