0

我有一个场景,我需要在表中插入一个 NULL 值。有人可以帮助我,我该怎么做。这不仅是在寻找使用 NVL 功能,而且任何东西都值得赞赏。

谢谢,库马尔

4

2 回答 2

1

听起来像你想要的NVL2

IE:

NVL2(your_var, 'val if not null', 'val if null')

或者也可以使用 case/decode

例如:

SQL>   var a varchar2(11)
SQL> exec :a := 'not null';

PL/SQL procedure successfully completed.

SQL> select nvl2(:a, 'c', 'b') "nvl2",
  2         case when :a is null then 'b' else 'c' end "case",
  3         decode(:a, null, 'b', 'c') "decode"
  4    from dual;

n c d
- - -
c c c

SQL> exec :a := '';

PL/SQL procedure successfully completed.

SQL> select nvl2(:a, 'c', 'b') "nvl2",
  2         case when :a is null then 'b' else 'c' end "case",
  3         decode(:a, null, 'b', 'c') "decode"
  4    from dual;

n c d
- - -
b b b

因此,如果您希望该值不为空,则为原样,如果为空,则需要硬编码:

nvl2(age, age, 42)

因此,如果age为 null,则为 42,否则为age.

于 2013-02-12T08:19:22.323 回答
0
Insert into mytable (ID, Name, Age) values (1,'John Smith', Null);
于 2013-02-11T22:42:34.283 回答