PL SQL 中声明时变量的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1349936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:23:04  来源:igfitidea点击:

default value of a variable at the time of declaration in PL SQL

sqloracleplsql

提问by tuinstoel

What is the default value of a variable VARCHAR2at the time of declaration in PL/SQL? Can I check it against NULLonce after I declare a variable?

VARCHAR2PL/SQL 中声明时变量的默认值是多少?我可以NULL在声明变量后检查一次吗?

回答by tuinstoel

Variables are default initialized with NULL.

变量默认初始化为 NULL。

You can change that, for example:

您可以更改它,例如:

create procedure show1
as
  l_start varchar2(10) := 'Hello';
begin
  if l_start is not null then 
    ....
  end if;
end;
/

You can also declare variables as not nullable:

您还可以将变量声明为不可为空:

create procedure show2
as
  l_start varchar2(10) not null := 'Hello';
begin
  null;
end;
/

回答by Andrew not the Saint

Defaults are NULL, you can use IS NULL or IS NOT NULL.

默认值为 NULL,您可以使用 IS NULL 或 IS NOT NULL。

回答by darreljnz

tuinstoel is correct.

tuinstoel 是正确的。

One addition: Don't be fooled into trying "ls_my_variable = NULL" as comparing with NULL always returns FALSE. Always use "ls_my_variable IS NULL" or "IS NOT NULL".

补充一点:不要被愚弄尝试“ls_my_variable = NULL”,因为与 NULL 比较总是返回 FALSE。始终使用“ls_my_variable IS NULL”或“IS NOT NULL”。

回答by Jim Hudson

And another small addition: if you're dealing with BLOBs (or CLOBS), "empty" is not the same as null. See the Oracle large objects manual if you need to.

还有一个小补充:如果您正在处理 BLOB(或 CLOBS),“空”与 null 不同。如果需要,请参阅 Oracle 大对象手册。