oracle tovarchar2_Oracle 运维之开发 varchar2 to clob 探究

本文介绍了在Oracle数据库中将VARCHAR2类型转换为CLOB的正确方法,包括通过先转换为LONG类型来实现转换的过程。文章指出,无论是VARCHAR2列有数据还是无数据,都不能直接转为CLOB,但可以通过先转换为LONG类型再转为CLOB。转换过程中需要注意数据完整性,转换后需重建索引。
摘要由CSDN通过智能技术生成

Varchar2

to long

类型

转换过程

列是否有数据

正确做法

varchar2

Varchar2—long

建立一个新的列,然后

long

Long---clob

是或否

原列的数据

copy

clob

过来。

总结:var char2 转换成clob 。varchar2 转换成 long 然后转换成 clob。

varcha2转换成long,转换列必须保证没有数据。

long转换成clob ,对列没有数据限制。

注意:你转换成功后,重建索引,以免索引失效。

转换的正确方法是:

// *Action:  Create a new column of the desired type and copy the current

//           column data to the new type using the appropriate type

//

constructor.

实验:

建立一张实验表,有varchar2类型的。

SQL> create table test (a varchar2(4000),b number);

Table created.

SQL> select * from test;

no rows selected

SQL> desc test;

Name

Null?

Type

----------------------------------------- -------- ----------------------------

A

VARCHAR2(4000)

B

NUMBER

插入数据

SQL> insert into test (A,B) values ('test',1);

1 row created.

SQL> select * from test;

A

--------------------------------------------------------------------------------

B

----------

test

1

SQL> commit;

Commit complete.

SQL>

alter table test modify(a colb);

alter table test modify(a colb)

*

ERROR at line 1:

ORA-22858: invalid alteration of datatype

// *Cause:   An attempt was made to modify the column type to object, REF,

//           nested table, VARRAY or LOB type.

// *Action:  Create a new column of the desired type and copy the current

//           column data to the new type using the appropriate type

//           constructor.

有人认为这个转换报错是因为列有数据造成的,其实不然。这个报错是因为lob类型的特殊性。即使你没有数据也不行,下面是证明列没有数据也不能转换的实验。

SQL> truncate table test;

Table truncated.

SQL> select

* from test;

no rows selected

SQL> alter table test modify(a clob);

alter table test modify(a clob)

*

ERROR at line 1:

ORA-22858: invalid alteration of datatype

以上步骤证明了,无聊你的varchar2 是否有数据 都不能直接转换成clob

但是可以转换成LONG 然后在转换成CLOB

Varchar2 to long

SQL> insert into test values ('test',1);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from test;

A

--------------------------------------------------------------------------------

B

----------

test

1

SQL> alter table test modify(a long);

alter table test modify(a long)

*

ERROR at line 1:

ORA-01439: column to be modified must be empty to change datatype

当varchar to log 时 ,需要列中没有数据。

由varchar2 转换成 long 类型成功。

SQL> truncate table test;

Table truncated.

SQL> alter table test modify(a long);

Table altered.

列有数据的情况下 long to clob也可以成功

,下面实验证明,然后验证数据是否正确。

SQL> insert into test (a,b) values ('test',1);

1 row created.

SQL> commit;

Commit complete.

SQL> alter table test modify(a clob);

Table altered.

SQL> select * from test;

A

--------------------------------------------------------------------------------

B

----------

test

1

数据正确没有丢失。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值