database Oracle:将 VARCHAR2 列更改为 CLOB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13402510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:46:46  来源:igfitidea点击:

Oracle: Changing VARCHAR2 column to CLOB

oracledatabase

提问by Maccath

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. Specifically, a serialized array.

我遇到了一个问题,我试图在 varchar2(4000) 列中存储的数据太大,因此我希望将该列更改为更适合存储大量文本数据的列。具体来说,一个序列化的数组。

  1. Firstly, is CLOB the best data type for me to use for this purpose? Is there a more appropriate data type?

  2. Secondly, when I try to alter the column using the usual snyntax:

    ALTER TABLE table MODIFY column CLOB
    

    I get the following error: ORA-22858: invalid alteration of datatype

    What's the most straightforward way to alter this table without losing any data?

  1. 首先,CLOB 是我为此目的使用的最佳数据类型吗?有没有更合适的数据类型?

  2. 其次,当我尝试使用通常的语法更改列时:

    ALTER TABLE table MODIFY column CLOB
    

    我收到以下错误:ORA-22858:数据类型的无效更改

    在不丢失任何数据的情况下更改此表的最直接方法是什么?

回答by a_horse_with_no_name

The most straightforward way, given that the operation of moving from a varcharcolumn to a CLOBis disallowed, would be to create a new column and move the data from the old column to the new column:

考虑varcharCLOB不允许从列移动到 a 的操作,最直接的方法是创建一个新列并将数据从旧列移动到新列:

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;

回答by Ausaf

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.
  • Convert column datatype from LONG to CLOB.
  • 将列数据类型从 VARCHAR2 转换为 LONG。
  • 将列数据类型从 LONG 转换为 CLOB。
ALTER TABLE table MODIFY column long;
ALTER TABLE table MODIFY column clob;

回答by user11815390

For Oracle 11g:

对于 Oracle 11g:

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