我有一个在Oracle数据库中设计为varchar类型的列details,这个数据库现在正在为客户使用,一些行已经存储了数据。
现在我想将details列更改为Clob列。有什么明智的方法可以完成这个任务吗?
(如前面的答案)这里是代码:
ALTER TABLE atable
ADD (tmpdetails CLOB);
UPDATE atable SET tmpdetails=details;
COMMIT;
ALTER TABLE atable DROP COLUMN details;
ALTER TABLE atable
RENAME COLUMN tmpdetails TO details;
但这样做不会保持您列的位置,它将把您的列移动到表格的末尾。因此,如果您想保持您的列的位置,请按照以下步骤操作。
alter table atable add (tempdetails varchar2(4000));
update atable set tempdetails = details;
update atable set details = null; -- this is necessary to change data type
alter table atable modify details long; -- this is required because you can not change directly to clob.
alter table atable modify details clob;
update atable set details=tempdetails;
alter table atable drop column tempdetails;
这是一种方法,可以在更改数据类型后保持列的数据和位置不变。有关详细信息和示例,请参见此处:http://www.oraclebin.com/2012/12/how-to-change-varchar2-to-clob-datatype.html
如果您需要在过程中访问表数据,请查看DBMS_REDEFINITION
请参考asktom上的类似问题 http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1770086700346491686