在oracle中对long数据类型的字段进行操作时,需要注意: long无法直接转化成char/varchar2字符类型,需要先转成clob,才能转成char/varchar2 注意官方文档中关于具体函数的使用方式,其参数的数据类型错误可能导致查询结果的错误! 如下实验: (1)在19c版本的库中,查看某个long数据类型的列,确认值不为空: Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production Version 19.3.0.0.0 SQL> select high_value 2 from dba_tab_partitions where high_value is not null and rownum<2; HIGH_VALUE -------------------------------------------------------------------------------- TO_DATE(' 2010-01-01 01:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA (2)创建varchar2字符类型的列,使用to_lob对该字段进行转化,并存储字符型字段中,结果错误(为空) SQL> create table ta (high_value varchar2(2000)); Table created. SQL> insert into ta 2 select to_lob(high_value) 3 from dba_tab_partitions where high_value is not null and rownum<2; 1 rows created. SQL> commit; Commit complete. SQL> select * from ta; HIGH_VALUE -------------------------------------------------------------------------------- SQL> (3)内层使用to_lob,外层使用to_char函数,报错 SQL> insert into ta 2 select to_char(to_lob(high_value)) 3 from dba_tab_partitions where high_value is not null and rownum<2; select to_char(to_lob(high_value)) * ERROR at line 2: ORA-00932: inconsistent datatypes: expected - got LONG (4)将to_lob的结果存入clob类型的字段中,结果正确 SQL> create table tb (high_value clob); Table created. SQL> insert into tb 2 select to_lob(high_value) 3 from dba_tab_partitions where high_value is not null and rownum<2; 1 rows created. SQL> SQL> commit; Commit complete. SQL> select * from tb; HIGH_VALUE -------------------------------------------------------------------------------- TO_DATE(' 2010-01-01 01:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA SQL> 总结: oracle数据库内部很多字段仍然使用LONG类型存储数据,为了将该结果进行字符拼接,可以创建表,将该字段存储CLOB字段中,再进行查询时即可使用to_char函数改为字符类型了。 步骤2中的错误结果的确容易造成误导,但官方文档中to_lob函数的要求是结果只能放入clob,严格按文档来使用是没问题的。 TO_LOB函数的说明: TO_LOB converts LONG or LONG RAW values in the column long_column to LOB values. You can apply this function only to a LONG or LONG RAW column, and only in the select list of a subquery in an INSERT statement. Before using this function, you must create a LOB column to receive the converted LONG values. To convert LONG values, create a CLOB column. To convert LONG RAW values, create a BLOB column. You cannot use the TO_LOB function to convert a LONG column to a LOB column in the subquery of a CREATE TABLE ... AS SELECT statement if you are creating an index-organized table. Instead, create the index-organized table without the LONG column, and then use the TO_LOB function in an INSERT ... AS SELECT statement. You cannot use this function within a PL/SQL package. Instead use the TO_CLOB (character) or TO_BLOB (raw) functions. TO_CHAR函数的参数有以下四种: TO_CHAR (bfile|blob) TO_CHAR (character) TO_CHAR (datetime) TO_CHAR (number) 其中TO_CHAR (character)的说明为: TO_CHAR (character) converts NCHAR, NVARCHAR2, CLOB, or NCLOB data to the database character set. The value returned is always VARCHAR2. When you use this function to convert a character LOB into the database character set, if the LOB value to be converted is larger than the target type, then the database returns an error.
oracle中long数据类型的一个转换错误以及如何转为字符类型
来源:这里教程网
时间:2026-03-03 16:15:21
作者:
编辑推荐:
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- Oracle DG同步失败故障处理(二)
Oracle DG同步失败故障处理(二)
26-03-03 - GoldenGate Enterprise Manager Plug-In(12.1.0.3.0) 部署文档
- 当 RPA 遇见人工智能 京东 RPA 实现 500% 效率提升
当 RPA 遇见人工智能 京东 RPA 实现 500% 效率提升
26-03-03 - 作为一个 DBA 常用的软件工具有哪些?
作为一个 DBA 常用的软件工具有哪些?
26-03-03 - Oracle服务器开启HugePages以支持大内存SGA
Oracle服务器开启HugePages以支持大内存SGA
26-03-03 - Oracle 恶意攻击问题分析和解决(一)
Oracle 恶意攻击问题分析和解决(一)
26-03-03 - 记一次12c pdb打补丁失败处理过程
记一次12c pdb打补丁失败处理过程
26-03-03 - 关于Heap中的一些概念
关于Heap中的一些概念
26-03-03 - 如何提高抖音直播间人气
如何提高抖音直播间人气
26-03-03 - oracle rac 打PSU补丁30805461两个问题(Java版本及空间不足导致失败)
