如果我的 Boolean 参数在JasperReports中为真,如何在 SQL 中添加 sub where 语句?
例如,我的 SQL 如下:
SELECT * FROM shops WHERE region = "Canada" ORDER BY name
如果我的参数为真,我想and isactive = 'Y'
在ORDER BY
.
有谁知道我怎么能做到这一点?
如果我的 Boolean 参数在JasperReports中为真,如何在 SQL 中添加 sub where 语句?
例如,我的 SQL 如下:
SELECT * FROM shops WHERE region = "Canada" ORDER BY name
如果我的参数为真,我想and isactive = 'Y'
在ORDER BY
.
有谁知道我怎么能做到这一点?
您可以添加附加参数以设置附加子句值。之后,您可以$P!{}
在查询中使用语法。
样品:
<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>
试试这个
select * from shops where region = "Canada"
AND isActive = CASE WHEN @var = 'True' then 'Y' ELSE isActive END
order by name
您可以直接在 SQL 中执行此操作
SELECT * FROM shops WHERE region = "Canada"
AND (@param <> `True` OR isActive = 'Y')
-----^^^^^^^^^^^^^^^^--<< Condition: Your param is not true------
ORDER BY name