ORA-22858: invalid alteration of datatype(varchar2转化clob)_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2279 | 回复: 0   主题:  ORA-22858: invalid alteration of datatype(varchar2转化clob)        下一篇 
yanpeng
注册用户
等级:上等兵
经验:127
发帖:68
精华:0
注册:2011-12-26
状态:离线
发送短消息息给yanpeng 加好友    发送短消息息给yanpeng 发消息
发表于: IP:您无权察看 2015-5-20 16:13:25 | [全部帖] [楼主帖] 楼主

ORA-22858: invalid alteration of datatype


在oracle里如何把table的varchar2类型转化为clob类型,其实标量类型的变量可以直接转换。

在'列'没有数据时,可以直接修改

alter table table_name modify column_name new_databyte;


在有数据的时候,可以删除和新建列或者通过一个中间列来修改原列的类型

1.

alter table table_name add column_new number;
update table_name set  column_new=column_old;
alter table table_name drop column column_old;
alter table table_name rename column  column_new to column_old;


例子:

查看现有表结构:

SQL> desc test_skate;
Name Type   Nullable Default Comments
---- ------ -------- ------- --------
NAME CLOB   Y
ID   NUMBER Y


添加新列:

SQL> alter table test_skate add id_new varchar(50);
Table altered


备份原列值:

SQL> update test_skate set id_new=id;
1 row updated


删除原列

SQL> alter table test_skate drop column id;
Table altered


把新列重命名为原列名:

SQL> alter table test_skate rename column id_new to id;
Table altered
SQL> desc test_skate;
Name Type         Nullable Default Comments
---- ------------ -------- ------- --------
NAME CLOB         Y
ID   VARCHAR2(50) Y


这种方法是把新增加的字段重命名为要转换的字段,字段的顺序发生了变化,如果要求字段顺讯不变,那就还要使用原来的字段,思路是先把现在列的数据转移到新增加的列上,然后修改原列的类型,再把数据转移回来,对原列数据要更新两次。如果数据量比较大的话,会产生大量的undo和redo;也会产生大量的阻塞;如果想对现有系统影响最小
那就采用表在线重定义的方式

alter table table_name  add cloumn_new number;
alter table table_name  modify cloumn_old null;
update  table_name  set cloumn_new=cloumn_old,cloumn_old=null;
commit;
alter table table_name  modify cloumn_old number(10,2);
update table_name  set cloumn_old=cloumn_new,cloumn_new=null;
commit;
alter table  tb_test  drop column cloumn_new;
alter table  tb_test  modify cloumn_old not null;
select * from  tb_test ;


例子:

查看现有表结构:

SQL> desc test_skate;
Name Type          Nullable Default Comments
---- ------------- -------- ------- --------
NAME CLOB          Y
ID   VARCHAR2(50)  Y
TEXT VARCHAR2(100) Y
SQL> select * from test_skate;
NAME     ID          TEXT
----- --------------------
1       222
1       222
1       222
1       222


添加新列

SQL> alter table test_skate add id_new varchar(100);
Table altered


编辑原列可以为null

SQL> alter table test_skate modify id null;
alter table test_skate modify id null
ORA-01451: column to be modified to NULL cannot be modified to NULL
SQL> alter table test_skate modify id not null;
Table altered
SQL> alter table test_skate modify id null;
Table altered


把原列数据复制到新列

SQL> update test_skate set id_new=id,id=null;
4 rows updated
SQL> commit;
Commit complete


更该原列的数据类型

SQL>  alter table test_skate modify  id number;
Table altered


把新列的数据复制到原列

SQL> update test_skate set id=id_new,id_new=null;
4 rows updated
SQL> commit;
Commit complete


删除新列

SQL> alter table test_skate drop column id_new;
Table altered


编辑原列不为null

SQL> alter table test_skate modify id not null;
Table altered


查看现有结构

SQL> desc test_skate;
Name Type          Nullable Default Comments
---- ------------- -------- ------- --------
NAME CLOB          Y
ID   NUMBER
TEXT VARCHAR2(100) Y
SQL>


但是如果要把varchar2类型转化为log类型,就要用些特殊手段了


例如:

SQL> create table test_skate
2  (
3    name varchar2(4000)
4  )
5  tablespace TBS_ARENA
6  ;
Table created


及时表为空,也不让更改

SQL> alter table test_skate modify name clob;
alter table test_skate modify name clob
ORA-22858: invalid alteration of datatype


这里可以借助long类型过度,如果记录为空,可以直接修改为LONG类型,在从long转化为clob,对于LONG类型,不管有没有数据存在,可以直接修改为CLOB类型

SQL> alter table test_skate modify name long;
Table altered
SQL> alter table test_skate modify name clob;
Table altered
SQL> desc test_skate;
Name Type Nullable Default Comments
---- ---- -------- ------- --------
NAME CLOB Y


--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论