为什么 SQLPLUS 将空和 null CLOB 显示为 null?

发布于 2024-10-14 16:51:04 字数 1081 浏览 2 评论 0原文

SQLPLUS 似乎以一种我没有预料到的方式显示 CLOB 的 null 和空字符串。

在 SQLPLUS 中尝试以下操作(我使用的是 Oracle 10g 服务器)。创建一个带有 CLOB 的表,并插入 null、空 clob 和我认为的空字符串:

create table tableA (field1 number, field2 clob);
insert into tableA values (1, null);
insert into tableA values (2, empty_clob());
insert into tableA values (3, '');

好的,让我们做一些查询,但首先我们需要告诉 SQLPLUS 为我们清楚地显示 null:

set null {NULL}

对于以下查询,我本以为只返回第 1 行,但它返回 2:

select * from tableA where field2 is null;

field1   field 2
-----------------------
1        {NULL} 
3        {NULL} 

嗯,所以 '' 在 CLOB 中存储为 null?

好的,根据该结果,我现在希望以下查询返回所有 3 行,但仅在第 1 行和第 3 行中显示 {NULL} 。然而我得到这个结果:

select * from tableA;

field1   field 2
-----------------------
1        {NULL} 
2        {NULL} 
3        {NULL} 

这很令人困惑。我认为只有 2 个空值,尽管我最初预计有 1 个空值。那么这里发生了什么? set null 不适用于 CLOB,如果是,我应该使用什么来代替?

我实际上正在尝试使用 null CLOB 值解决另一个问题,但这种令人困惑的行为让我运行了一段时间,所以我想在继续之前了解这一点。

预先感

谢博兹

It seems SQLPLUS shows nulls and empty strings for CLOBs in a way that I wasn't expecting.

Try the following in SQLPLUS (I'm using Oracle 10g Server). Create a table with a CLOB, and insert null, empty clob, and what I think of as an empty string:

create table tableA (field1 number, field2 clob);
insert into tableA values (1, null);
insert into tableA values (2, empty_clob());
insert into tableA values (3, '');

OK, lets do some queries, but first we need to tell SQLPLUS to show nulls clearly for us:

set null {NULL}

For the following query, I would have expected only row 1 returned, but it returns 2:

select * from tableA where field2 is null;

field1   field 2
-----------------------
1        {NULL} 
3        {NULL} 

Hmm, so '' is stored as a null in a CLOB?

Ok, so based on that result, I would now expect the following query to return all 3 rows but show {NULL} in rows 1 and 3 only. However I get this result:

select * from tableA;

field1   field 2
-----------------------
1        {NULL} 
2        {NULL} 
3        {NULL} 

This is confusing. I thought there were only 2 nulls, even though I originally expected 1. So what's going on here? Does set null not work for CLOBs, and if so what should I use instead?

I'm actually trying to solve a different problem with null CLOB values, but this confusing behaviour has had me running rings for a while, so I'd like to understand this before I continue.

Thanks in advance

Boz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

真心难拥有 2024-10-21 16:51:04

Oracle 不区分 NULL 和空字符串。这是众所周知的违反 SQL 标准的行为。

这就是默认字符串类型为 VARCHAR2 而不是 VARCHAR 的原因。

在当前发布的所有版本中它们都是相同的,但不建议使用VARCHAR。这是因为 VARCHAR 应该区分 NULL 和空字符串,而标准中没有描述的 VARCHAR2 则不能。

Oracle does not distinguish between NULL and an empty string. This is a well-known violation or SQL standards.

This is the reason of default string type being VARCHAR2 and not VARCHAR.

In all currently released versions they are the same, but VARCHAR is not recommended for use. This is because VARCHAR is supposed to distinguish between NULL and an empty string, while VARCHAR2, not being described in the standards, is not.

浪菊怪哟 2024-10-21 16:51:04

我认为这与 SQL*Plus 处理空字符串和空值的方式有关。

empty_clob() 不会创建 NULL 值,而是创建长度为零的 CLOB。

当您查询 field2 为空的行时,不会返回第 2 行,这一事实证明了这一点。

使用我的 JDBC 工具,我可以用不同的颜色突出显示为空的列,并且带有empty_clob() 的行不会突出显示,而其他两行则突出显示。

所以我想说这是对 SQL*Plus 中 set null 选项的错误处理。通过以下语句,您将看到差异:

select field1, 
       nvl(field2, 'THIS IS NULL')
from tableA;

对我来说,这显示:

field1   field 2
-----------------------
1        THIS IS NULL 
2        
3        THIS IS NULL

I think it's something to do with the way SQL*Plus handles empty strings and null values.

empty_clob() does not create a NULL value, but a CLOB of length zero.

Which is proven by the fact that row 2 is is not returned when you query for rows where field2 is null.

With my JDBC tool I can highlight columns that are null with a different color, and there the row with the empty_clob() is not highlighted whereas the other two are.

So I'd say it's a wrong handling of the set null option in SQL*Plus. With the following statement you will see the difference:

select field1, 
       nvl(field2, 'THIS IS NULL')
from tableA;

For me this displays:

field1   field 2
-----------------------
1        THIS IS NULL 
2        
3        THIS IS NULL
吹泡泡o 2024-10-21 16:51:04

看看第 2 行中的实际内容会很有趣。根据 文档(见下文)empty_clob() 将返回一个没有数据的初始化 clob 定位(对于 null 问题有些不清楚)。

当在 SQL 查询工具中执行 SQL 语句时,Oracle 倾向于隐式地将 CLOB 转换为以任意长度截断的字符串。

用途

EMPTY_BLOB 和 EMPTY_CLOB 返回一个空 LOB 定位器,可用于初始化 LOB 变量,或者在 INSERT 或 UPDATE 语句中将 LOB 列或属性初始化为 EMPTY。 EMPTY 表示 LOB 已初始化,但未填充数据。

It would be interesting to see what is really in row 2. It may not truly be null as according to the documentation (see below) empty_clob() will return a initialized clob locate that does not have data (somewhat unclear on the null issue).

When executing SQL statements in SQL query tools though Oracle tends to implicitly convert CLOB's to strings which are cut off at some arbitrary length.

Purpose

EMPTY_BLOB and EMPTY_CLOB return an empty LOB locator that can be used to initialize a LOB variable or, in an INSERT or UPDATE statement, to initialize a LOB column or attribute to EMPTY. EMPTY means that the LOB is initialized, but not populated with data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文