4

このクエリを試していますが、成功しません。

SELECT name, phone_office, billing_address_city, billing_address_street, billing_address_country 
FROM accounts
WHERE ($P!{EmployeeID} is null or assigned_user_id = $P!{EmployeeID})
ORDER BY billing_address_country, billing_address_city

この URL は EmployeeID でフィルタリングされ、正常に機能します。

.../flow.html?_flowId=viewReportFlow&reportUnit=/reports/samples/EmployeeAccounts&EmployeeID=sarah_id

しかし、EmployeeID パラメーターを削除すると、フィルターを削除したい場所があります。したがって、すべての結果が表示されます。

.../flow.html?_flowId=viewReportFlow&reportUnit=/reports/samples/EmployeeAccounts

私の質問は、SQL クエリでオプションの where を渡す正しい方法は何ですか。

4

2 に答える 2

11

わかりました、サンプルを見てみましょう。

たとえば、次のクエリがあります。

SELECT id, city, street FROM address WHERE city=$P{inputParamCity} ORDER BY city

しかし、inputParamCityは未定義にすることができます。この場合、エラーが発生しました。

Error filling print... Error preparing statement for executing the report query : 
SELECT id, city, street FROM address WHERE city=? ORDER BY city

どうすれば修正できますか?
これは非常に簡単です。次のように、デフォルトの式で別のパラメータを追加できます。

<parameter name="whereClause" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA[()$P{inputParamCity} == null || $P{inputParamCity}.isEmpty()) ? "1=1" : "city='" + $P{inputParamCity} + "'"]]></defaultValueExpression>
</parameter>

-inputParamCityパラメーターが定義されていない場合は、「偽の」句「1=1」が使用されます。それ以外の場合は、都市フィールドによるフィルターが適用されます。

もちろん、この新しいパラメータを使用するには、クエリ式を変更する必要があります。この場合のクエリ式は次のようになります。

<queryString>
    <![CDATA[SELECT id, city, street FROM address WHERE $P!{whereClause} ORDER BY city]]>
</queryString>

標本、見本

jrxmlファイル:

<?xml version="1.0" encoding="UTF-8"?>
<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="optional_where_clause" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d3648644-0087-4dfc-ac6d-87e82d9bb33e">
    <parameter name="inputParamCity" class="java.lang.String"/>
    <parameter name="whereClause" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[($P{inputParamCity} == null || $P{inputParamCity}.isEmpty()) ? "1=1" : "city='" + $P{inputParamCity} + "'"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT id, city, street FROM address WHERE $P!{whereClause} ORDER BY city]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="CITY" class="java.lang.String"/>
    <field name="STREET" class="java.lang.String"/>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="c2a80b99-e087-4839-8e77-841edd899255" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="0aafcfd6-60f7-4272-8e7d-0aa77507204b" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="c8726513-8250-43ec-bafc-003e81094c27" x="200" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

未定義のinputParamCityパラメータ (値が設定されていない) を使用した場合、結果は次のようになります。

ここに画像の説明を入力

この場合、エンジンによって使用されたクエリは次のとおりです。

SELECT id, city, street FROM address WHERE 1=1 ORDER BY city

たとえば、inputParamCityパラメーターに値Chicagoを設定すると、結果は次のようになります。

ここに画像の説明を入力

この場合、エンジンによって使用されたクエリは次のとおりです。

SELECT id, city, street FROM address WHERE city='Chicago' ORDER BY city

ノート:

  • 詳細については、次の投稿を参照してください: JasperReports: Passing parameters to query

  • whereClauseパラメータの式とクエリ式を変更できます。たとえば、WHEREキーワードをクエリ式からパラメーターの式に移動して、偽の「1=1」の使用を防ぐことができます。

于 2013-10-16T18:37:13.013 に答える