51

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?

1
  • 1
    Make sure to commit BEFORE droping the column. Or copy operation will not be commited and you will loose all your data.
    – Oscar
    Jun 2, 2017 at 13:05

3 Answers 3

74

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;
1
  • Yep. The OP's syntax was right but the operation is just not allowed. Nov 15, 2012 at 17:06
11

The VARCHAR2 column cannot be directly converted to CLOB but it can be done in 2 steps:

  • Convert column datatype from VARCHAR2 to LONG.
  • Convert column datatype from LONG to CLOB.
ALTER TABLE table MODIFY column long;
ALTER TABLE table MODIFY column clob;
1
  • 1
    ORA-01439: column to be modified must be empty to change datatype
    – Hawk
    Jul 26 at 13:54
3

For Oracle 11g:

ALTER TABLE table MODIFY column long;
ALTER TABLE table MODIFY column clob;
1
  • Isn't that exactly what OP was trying?
    – Bob
    Jul 21, 2019 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.