如何获取 Oracle 中 CLOB 列的大小(以字节为单位)?
如何获取 Oracle 中 CLOB
列的大小(以字节为单位)?
LENGTH()
和 DBMS_LOB.getLength()
都返回 CLOB
中使用的字符数,但我需要知道使用了多少字节(我正在处理多字节字符集)。
How do I get the size in bytes of a CLOB
column in Oracle?
LENGTH()
and DBMS_LOB.getLength()
both return number of characters used in the CLOB
but I need to know how many bytes are used (I'm dealing with multibyte charactersets).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
经过一番思考,我想出了这个解决方案:
SUBSTR
仅返回前 4000 个字符(最大字符串大小)TO_CHAR
从CLOB
转换为VARCHAR2
LENGTHB
返回字符串使用的长度(以字节为单位)。After some thinking i came up with this solution:
SUBSTR
returns only the first 4000 characters (max string size)TO_CHAR
converts fromCLOB
toVARCHAR2
LENGTHB
returns the length in Bytes used by the string.我将我的评论添加为答案,因为它解决了比接受的答案更广泛的情况下的原始问题。注意:您仍然必须知道数据的最大长度和多字节字符的大致比例。
如果 CLOB 大于 4000 字节,则需要使用 DBMS_LOB.SUBSTR 而不是 SUBSTR。 请注意,金额和偏移参数在DBMS_LOB.SUBSTR中是相反的。
接下来,您可能需要对小于4000的金额进行子字符串化,因为这参数是字符数,如果你有多字节字符,那么4000个字符将超过4000字节长,你会得到
ORA- 06502:PL/SQL:数字或值错误:字符串缓冲区太小
,因为子字符串结果需要适合 VARCHAR2,该 VARCHAR2 具有 4000 字节的限制。您可以检索的确切字符数取决于数据中每个字符的平均字节数。所以我的答案是:
您可以根据需要添加尽可能多的块来覆盖最长的 CLOB,并根据数据的平均每个字符字节调整块大小。
I'm adding my comment as an answer because it solves the original problem for a wider range of cases than the accepted answer. Note: you must still know the maximum length and the approximate proportion of multi-byte characters that your data will have.
If you have a CLOB greater than 4000 bytes, you need to use DBMS_LOB.SUBSTR rather than SUBSTR. Note that the amount and offset parameters are reversed in DBMS_LOB.SUBSTR.
Next, you may need to substring an amount less than 4000, because this parameter is the number of characters, and if you have multi-byte characters then 4000 characters will be more than 4000 bytes long, and you'll get
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
because the substring result needs to fit in a VARCHAR2 which has a 4000 byte limit. Exactly how many characters you can retrieve depends on the average number of bytes per character in your data.So my answer is:
where you add as many chunks as you need to cover your longest CLOB, and adjust the chunk size according to average bytes-per-character of your data.
对于大于 VARCHAR2 的 CLOB 大小,请尝试此操作:
我们必须将 CLOB 拆分为“VARCHAR2 兼容”大小的部分,对 CLOB 数据的每个部分运行 lengthb,并汇总所有结果。
Try this one for CLOB sizes bigger than VARCHAR2:
We have to split the CLOB in parts of "VARCHAR2 compatible" sizes, run lengthb through every part of the CLOB data, and summarize all results.
简单的解决方案是将 CLOB 转换为 BLOB,然后请求 BLOB 的长度!
问题是Oracle没有将CLOB转换为BLOB的函数,但我们可以简单地定义一个函数来执行此操作
用于获取字节数的SQL命令是
或者
我已经在Oracle 10g上测试了这个而不使用Unicode( UTF-8)。
但我认为这个解决方案使用 Unicode(UTF-8) Oracle 实例必须是正确的:-)
我想要渲染,感谢 Nashev,他发布了一个将 clob 转换为 blob 的解决方案 如何在 Oracle 中将 CLOB 转换为 BLOB? 以及这篇用德语编写的文章(代码为 PL/SQL)13ter.info.blog 还提供了将 blob 转换为 clob 的功能!
有人可以测试 Unicode(UTF-8) CLOB 中的 2 个命令,以便我确定这适用于 Unicode 吗?
The simple solution is to cast CLOB to BLOB and then request length of BLOB !
The problem is that Oracle doesn't have a function that cast CLOB to BLOB, but we can simply define a function to do that
The SQL command to use to obtain number of bytes is
or
I have tested this on Oracle 10g without using Unicode(UTF-8).
But I think that this solution must be correct using Unicode(UTF-8) Oracle instance :-)
I want render thanks to Nashev that has posted a solution to convert clob to blob How convert CLOB to BLOB in Oracle? and to this post written in german (the code is in PL/SQL) 13ter.info.blog that give additionally a function to convert blob to clob !
Can somebody test the 2 commands in Unicode(UTF-8) CLOB so I'm sure that this works with Unicode ?
NVL(length(clob_col_name),0) 对我有用。
NVL(length(clob_col_name),0) works for me.
使用表名从 dba_lobs 检查 LOB 段名称。
现在使用段名称查找 dba_segments 中使用的字节。
Check the LOB segment name from dba_lobs using the table name.
Now use the segment name to find the bytes used in dba_segments.
它只能工作到 4000 字节,如果 clob 大于 4000 字节,那么我们使用这个
或
It only works till 4000 byte, What if the clob is bigger than 4000 bytes then we use this
or