如何在 HQL 中模拟 NVL [英] How to simulate NVL in HQL
本文介绍了如何在 HQL 中模拟 NVL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过了:
from Table where (:par1 is null or col1 = :par1)
但碰巧是
from Table where :par1 is null
总是返回表的所有行,即使 :par1 不为空.
always returns all the rows of the table, even if the :par1 is not null.
同时
select * from table where col1 = 'asdf'
不返回任何行.
我不能使用原生语法,因为我的应用程序应该在不同的数据库引擎上运行
I can't use native grammars because my application is supposed to run on different database engines
推荐答案
与 HQL 中的 nvl
命令等效的是 coalesce
命令.如果a
不为空,coalesce(a,b)
将返回a
,否则返回b
.
The equivalent to the nvl
command in HQL is the coalesce
command. coalesce(a,b)
will return a
if a
is not null, otherwise b
.
所以你会想要一些东西:
So you would want something on the lines of:
from Table where col1 = coalesce(:par1, 'asdf')
这篇关于如何在 HQL 中模拟 NVL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文