Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  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?

like image 910
Maccath Avatar asked Nov 15 '12 17:11

Maccath


People also ask

Can we change VARCHAR2 to CLOB in Oracle?

just create new table as select from the old (using to_lob on the column) and then index, constrain, etc the new table, then drop old, rename new to old.

How do I add a column to a CLOB in Oracle?

Type an "ALTER TABLE" command to add a CLOB item to an existing table, using the following SQL code as a guide: ALTER TABLE your_table ( add big_text_field CLOB ); Press "Enter" to execute the command.


1 Answers

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:

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; 
like image 174
a_horse_with_no_name Avatar answered Sep 19 '22 01:09

a_horse_with_no_name