varchar2 字段转换成lob类型

   
   
 

总结:有两种方法将字段的varchar2转换成lob类型

 

方法一是:借助于临时中间列 由varchar2列的数据放到中间列,然后清空varchar2列, 转换成long类型,再由long类型转换成clob类型,把中间列的数据放到clob列,删除临时中间列;

方法二是在线冲定义,内部方法是一样的,都是借助于中间表,对表中的列进行重新定义;

varchar2----long-----clob

create table tt1(a varchar2(10),n number);
insert into tt1 values('aa',1);
insert into tt1 values('bb',2);
insert into tt1 values('cc',3);
commit;

SQL> select * From tt1;

A      N
---------- ----------
aa      1
bb      2
cc      3


SQL> desc tt1;
 Name                 Null?    Type
 ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
 A                   VARCHAR2(10)
 N                   NUMBER

首先尝试从varchar2 转换到lob类型,不行

SQL> alter table tt1 modify a long;
alter table tt1 modify a long
                       *
ERROR at line 1:
ORA-01439: column to be modified must be empty to change datatype

 

 表里有数据从varchar2转换成long类型 不行

 SQL> alter table t1 modify a long;
alter table t1 modify a long
*
ERROR at line 1:
ORA-01439: column to be modified must be empty to change datatype

 

 

alter table tt1 add b varchar2(10);

 

SQL> update tt1 set b=a;

3 rows updated.

SQL> commit;

Commit complete.

SQL> select * From tt1;

A      N              B
---------- ---------- ----------
aa      1                aa
bb      2               bb
cc      3               cc


清空表里的数据从varchar2 转换成long 类型,可以

update tt1 set  a ='';


表里有数据从varchar2转换成long类型 不行

alter table tt1 modify a long;
alter table tt1 modfify a clob;
update tt1 set a=b;

SQL> select * From tt1;

A             N B
-------------------------------------------------------------------------------- ---------- ----------
aa             1 aa
bb             2 bb
cc             3 cc

SQL> desc tt1
 Name                 Null?    Type
 ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
 A                   CLOB
 N                   NUMBER
 B                   VARCHAR2(10)

SQL> alter table tt1 drop column b;

Table altered.

SQL> select * From tt1;

A             N
-------------------------------------------------------------------------------- ----------
aa             1
bb             2
cc             3

 

或者

SQL> create table tt1(a varchar2(10),n number);

表已创建。

SQL>  insert into tt1 values('aa',1);

已创建 1 行。

SQL>  insert into tt1 values('bb',2);

已创建 1 行。

SQL>  insert into tt1 values('cc',3);

已创建 1 行。

SQL> commit;

提交完成。

SQL> select *From tt1;

A                   N
---------- ----------
aa                  1
bb                  2
cc                  3


SQL> alter table tt1 add ( n1 clob);

表已更改

SQL> set linesize 200
SQL> /

A                   N N1
---------- ---------- ----------------------------------------------------------------
aa                  1
bb                  2
cc                  3

SQL> update tt1 set n1=n;

已更新3行。

SQL> commit;

提交完成。

SQL> select *From tt1;

A                   N N1
---------- ---------- ----------------------------------------------------------------
aa                  1 1
bb                  2 2
cc                  3 3

SQL> alter table tt1 drop column n;

表已更改。

SQL> select *From tt1;

A          N1
---------- ---------------------------------------------------------------------------
aa         1
bb         2
cc         3


SQL> alter table tt1 rename column n1  to n;

表已更改。

SQL> select *From tt1;

A          N
---------- ---------------------------------------------------------------------------
aa         1
bb         2
cc         3

 

 

 
 

 

 

可以利用在线冲定义:


SQL> create table t1(a varchar2(10),n number);

SQL> insert into t1 values('aa',1);

1 row created.

SQL> insert into t1 values('bb',2);

1 row created.

SQL> insert into t1 values('cc',3);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t1;

A N
-------------------------------------------------------------------------------- ----------
aa 1
bb 2
cc 3

 

SQL> create table tt1(a clob,n number);

Table created.


SQL>  create table tt1(a clob,n number);

表已创建。

SQL> EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE('SCOTT', 'T1', 2);

PL/SQL 过程已成功完成。

SQL> exec dbms_redefinition.start_redef_table('SCOTT','T1','TT1','to_clob(a) a,n n' ,2);

PL/SQL 过程已成功完成。

SQL> exec dbms_redefinition.sync_interim_table('SCOTT','T1','TT1');

PL/SQL 过程已成功完成。

SQL> exec dbms_redefinition.finish_redef_table('SCOTT','T1','TT1');

PL/SQL 过程已成功完成。

SQL> exec dbms_redefinition.ABORT_REDEF_TABLE('SCOTT','T1','TT1');

PL/SQL 过程已成功完成。

SQL> desc t1
ERROR:
ORA-04020: 尝试锁定对象 SCOTT.T1 时检测到死锁


SQL> commit;

提交完成。

SQL> desc t1
 名称                                                                                                      是否为空? 类型
 ----------------------------------------------------------------------------------------------------------------- -------- ------

 A                                                                                                                  CLOB
 N                                                                                                                  NUMBER

SQL> dec tt1

SQL> desc tt1
 名称                                                                                                      是否为空? 类型
 ----------------------------------------------------------------------------------------------------------------- -------- ------

 A                                                                                                                  VARCHAR2(10)
 N                                                                                                                  NUMBER

SQL> select *From t1
  2  ;

A                                                                                         N
-------------------------------------------------------------------------------- ----------
aa                                                                                        1
bb                                                                                        2
cc                                                                                        3

SQL> select *from tt1;

A                   N
---------- ----------
aa                  1
bb                  2
cc                  3

 

 

SQL> create table t1(a varchar2(10),n number);

表已创建。

SQL>  insert into t1 values('aa',1);

已创建 1 行。

SQL> insert into t1 values('bb',2);

已创建 1 行。

SQL>  insert into t1 values('cc',3);

已创建 1 行。

SQL>  commit;

提交完成。

SQL>  create table tt1(a clob,n number);

表已创建。


SQL> alter table t1 add primary key(n);

表已更改。

SQL> alter table tt1 add primary key(n);

表已更改。

SQL> EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE('SCOTT', 'T1', dbms_redefinition.cons_use_pk);

PL/SQL 过程已成功完成。

SQL> exec dbms_redefinition.start_redef_table('SCOTT','T1','TT1','to_clob(a) a,n n' );

PL/SQL 过程已成功完成。

SQL>  exec dbms_redefinition.sync_interim_table('SCOTT','T1','TT1');

PL/SQL 过程已成功完成。

SQL> exec dbms_redefinition.finish_redef_table('SCOTT','T1','TT1');

PL/SQL 过程已成功完成。

SQL> desc t1
 名称                                                                                                      是否为空? 类型
 ----------------------------------------------------------------------------------------------------------------- -------- ------

 A                                                                                                                  CLOB
 N                                                                                                         NOT NULL NUMBER

SQL> desc tt1
 名称                                                                                                      是否为空? 类型
 ----------------------------------------------------------------------------------------------------------------- -------- ------

 A                                                                                                                  VARCHAR2(10)
 N                                                                                                         NOT NULL NUMBER

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

lob类型不能做主键
SQL> ALTER TABLE T1 ADD CONSTRAINT PK PRIMARY KEY(A);
ALTER TABLE T1 ADD CONSTRAINT PK PRIMARY KEY(A)
                                             *
ERROR at line 1:
ORA-02329: column of datatype LOB cannot be unique or a primary key

 

 

 

 

SQL> ALTER TABLE T1 ADD N NUMBER;

Table altered.

 

SQL> select * From t1;

A             N
-------------------------------------------------------------------------------- ----------
aaaa
bbbbbb
cccccc
ddddd

SQL> update t1 set n=1 where A='aaaa';
update t1 set n=1 where A='aaaa'
                        *
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CLOB

 

SQL> update t1 set n=1 where a=to_lob('aaaa');
update t1 set n=1 where a=to_lob('aaaa')
                          *
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CHAR

 


SQL> insert into t1 values('eeeee',5);

1 row created.

SQL> commit;

Commit complete.

插入clob数据类型的直接以字符串形式就可以了,但是

 

 

clob不支持distinct 查询
SQL> select distinct a from t1;
select distinct a from t1
                *
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected - got CLOB


http://book.51cto.com/art/201202/319249.htm


 表里有数据从varchar2转换成long类型 不行

你可能感兴趣的:(varchar2 字段转换成lob类型)