JasperReports: How to add a WHERE statement in SQL query depending on a Boolean parameter?

前端 未结 3 932
刺人心
刺人心 2021-01-24 04:34

How can I add a sub where statement in SQL if my Boolean parameter is true in JasperReports?
For example, I have my SQL as below:

SELECT * FROM shop         


        
相关标签:
3条回答
  • 2021-01-24 04:50

    Try this

    select * from shops where region = "Canada"
    AND isActive = CASE WHEN @var = 'True' then 'Y' ELSE isActive END
    order by name
    
    0 讨论(0)
  • 2021-01-24 04:58

    You can add additional parameter for setting additional clause value. After that you can use $P!{} syntax in query.

    The sample:

    <parameter name="param" class="java.lang.Boolean"/>
    <parameter name="whereCond" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[$P{param} ? " AND isactive='Y'" : ""]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT * FROM shops WHERE region='Canada' $P!{whereCond} ORDER BY name]]>
    </queryString>
    
    0 讨论(0)
  • 2021-01-24 05:03

    You can do that direct in SQL

    SELECT * FROM shops WHERE region = "Canada"
    AND (@param <> `True` OR isActive = 'Y')
    -----^^^^^^^^^^^^^^^^--<< Condition: Your param is not true------
    ORDER BY name
    
    0 讨论(0)
提交回复
热议问题