检查plsql中变量是否为空
我想检查变量是否为空。如果它为空,那么我想为该变量设置一个值:
//data type of var is number
if Var = null then
var :=5;
endif
但我收到错误。如何检查变量是否为空?
我正在使用 oracle 数据类型
I want to check if a variable is null. If it is null, then I want to set a value to that variable:
//data type of var is number
if Var = null then
var :=5;
endif
But I am geting error in it. How can I check if a variable is null?
I am using oracle data type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用:
Oracle 9i+:
其他替代方案:
参考:
Use:
Oracle 9i+:
Other alternatives:
Reference:
在 PL/SQL 中,不能使用“=”或“<>”等运算符测试
NULL
,因为与NULL
的所有比较都会返回NULL
。要将某些内容与NULL
进行比较,您需要使用特殊运算符IS NULL
或IS NOT NULL
,它们正是用于此目的。 而不是编写因此,您应该编写
In the case you’ve made you also have the option of using the
NVL
内置函数, 。 NVL 采用两个参数,第一个是变量,第二个是值(常量或计算值)。NVL
查看其第一个参数,如果发现第一个参数为NULL
,则返回第二个参数。如果NVL
的第一个参数不是NULL
,则返回第一个参数。 你可以重写所以
,我希望这有帮助。
编辑
因为我写这个答案已经快十年了,所以让我们通过扩展它来庆祝一下。
COALESCE
函数是 Oracle 的NVL
的 ANSI 等效项。它与 NVL 的不同之处在于几个 IMO 好的方面:与
NVL
相比,COALESCE
仅在必须时评估参数,而NVL
评估其两个参数,然后确定第一个参数是否一个是 NULL,等等。所以COALESCE
可以更有效,因为它不会花时间评估不会使用的东西(并且可能会导致不需要的副作用),但这也意味着COALESCE
并不是NVL
的 100% 直接替代品。In PL/SQL you can't use operators such as '=' or '<>' to test for
NULL
because all comparisons toNULL
returnNULL
. To compare something againstNULL
you need to use the special operatorsIS NULL
orIS NOT NULL
which are there for precisely this purpose. Thus, instead of writingyou should write
In the case you've given you also have the option of using the
NVL
built-in function.NVL
takes two arguments, the first being a variable and the second being a value (constant or computed).NVL
looks at its first argument and, if it finds that the first argument isNULL
, returns the second argument. If the first argument toNVL
is notNULL
, the first argument is returned. So you could rewriteas
I hope this helps.
EDIT
And because it's nearly ten years since I wrote this answer, let's celebrate by expanding it just a bit.
The
COALESCE
function is the ANSI equivalent of Oracle'sNVL
. It differs fromNVL
in a couple of IMO good ways:It takes any number of arguments, and returns the first one which is not NULL. If all the arguments passed to
COALESCE
are NULL, it returns NULL.In contrast to
NVL
,COALESCE
only evaluates arguments if it must, whileNVL
evaluates both of its arguments and then determines if the first one is NULL, etc. SoCOALESCE
can be more efficient, because it doesn't spend time evaluating things which won't be used (and which can potentially cause unwanted side effects), but it also means thatCOALESCE
is not a 100% straightforward drop-in replacement forNVL
.使用 IS NULL
参考页面
Use IS NULL
reference page
还有 NVL 函数
http://www.techonthenet.com/oracle/functions/nvl .php
There's also the NVL function
http://www.techonthenet.com/oracle/functions/nvl.php
如果 var 为空则使用
use if var is null
始终记住要小心 pl/sql 条件子句中的 null,因为 null 永远不会大于、小于、等于或不等于任何内容。避免它们的最佳方法是使用 nvl。
例如,
Never 不会进入 if 子句。
这些会起作用的。
Always remember to be careful with nulls in pl/sql conditional clauses as null is never greater, smaller, equal or unequal to anything. Best way to avoid them is to use nvl.
For example
Never goes inside the if clause.
These would work.
另一种方式:
COALESCE 是 Oracle 的 NVL 函数的 ANSI 等效项(或多或少)。
Another way:
COALESCE is the ANSI equivalent (more or less) of Oracle's NVL function.