简体   繁体   English

Oracle:将VARCHAR2列更改为CLOB

[英]Oracle: Changing VARCHAR2 column to CLOB

I have an encountered an issue where the data I was trying to store in my varchar2(4000) column was too big, so I wish to change the column to one more suitable for storing large amounts of textual data. 我遇到一个问题,我试图存储在我的varchar2(4000)列中的数据太大,所以我希望将列更改为更适合存储大量文本数据的列。 Specifically, a serialized array. 具体来说,是一个序列化数组。

  1. Firstly, is CLOB the best data type for me to use for this purpose? 首先,CLOB是我用于此目的的最佳数据类型吗? Is there a more appropriate data type? 有更合适的数据类型吗?

  2. Secondly, when I try to alter the column using the usual snyntax: 其次,当我尝试使用通常的snyntax改变列时:

     ALTER TABLE table MODIFY column CLOB 

    I get the following error: ORA-22858: invalid alteration of datatype 我收到以下错误: ORA-22858:数据类型的无效更改

    What's the most straightforward way to alter this table without losing any data? 在不丢失任何数据的情况下,更改此表的最简单方法是什么?

The most straightforward way, given that the operation of moving from a varchar column to a CLOB is disallowed, would be to create a new column and move the data from the old column to the new column: 考虑到不允许从varchar列移动到CLOB的操作,最简单的方法是创建一个新列并将数据从旧列移动到新列:

ALTER TABLE some_table ADD (foo CLOB);
UPDATE some_table SET foo = old_column;
ALTER TABLE some_table DROP COLUMN old_column;
ALTER TABLE some_table RENAME COLUMN foo TO old_column;

The VARCHAR2 column cannot be directly converted to CLOB but it can be done in 2 steps: VARCHAR2列无法直接转换为CLOB,但可以分两步完成:

  • Convert column datatype from VARCHAR2 to LONG. 将列数据类型从VARCHAR2转换为LONG。
  • Convert column datatype from LONG to CLOB. 将列数据类型从LONG转换为CLOB。
ALTER TABLE table MODIFY column long;
ALTER TABLE table MODIFY column clob;

For Oracle 11g: 对于Oracle 11g:

ALTER TABLE table MODIFY column long;
ALTER TABLE table MODIFY column clob;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM