43

I have a column details designed as varchar in oracle DB, this DB is being used now for customers and some rows already have data stored.

Now I want to change the column details to a Clob column. What is a smart way to accomplish this?

4 Answers 4

94

(as the previous answer) and here's the code:

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;
3
  • 7
    +1 If we're doing this in a single session the COMMIT is unnecessary, as the subsequent ALTER TABLE statement issues an implicit commit (two, actually).
    – APC
    Jul 27, 2011 at 8:16
  • 1
    But this will not maintain the position of your column. It will move your column to the end of table. So if you want to maintain the position of your column as well follow these steps. Jan 25, 2013 at 4:34
  • 4
    Sorry but, I was simply answering the question, I guess if you are using a select * into a table then the column order would be important but I would first question the sanity of such an approach. Jan 25, 2013 at 14:43
15
  1. Add a clob column to the table
  2. update clob column with values from varchar column
  3. drop varchar column
  4. rename clob column to varchar columns name
15

But this will not maintain the position of your column. It will move your column to the end of table. So if you want to maintain the position of your column as well follow these steps.

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;

This is the way in which you will maintain the data and position of your column intact even after changing the datatype. For detail information with example see here : http://www.oraclebin.com/2012/12/how-to-change-varchar2-to-clob-datatype.html

4
  • Hi, this answer is valid but the 3rd statement must be changed as follows: update atable set details = null; -- this is necessary to change data type
    – Marco Z.
    Aug 25, 2016 at 12:16
  • This has caused my indexes on the table to be in UNUSABLE state.
    – saran3h
    Aug 26, 2020 at 6:45
  • This link uses the same solution additionally taking care about indexes.
    – Pavel S.
    Apr 12, 2021 at 13:11
  • For me, this approach didn't work because after executing the script even if the column was changed correctly, the table was finally corrupted! And I had to delete the table and recreate it! Apr 14, 2021 at 15:44
1

if you need your table data to be accessible during the process.. look at DBMS_REDEFINITION

see a similar question on asktom http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1770086700346491686

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.