Modify VARCHAR2 datatype to CLOB in OracleDB

It is a common scenario where you do not design your database very well in the beginning of a project. Therefore you have to do some changes and alter tables. One common place for changes is the datatype of your column. You may need to change either its precision or the whole type. In most […]

by Denis Danov

June 12, 2014

2 min read

Oracle DB 11gR2 Win32 Windows2003 01 - Modify VARCHAR2 datatype to CLOB in OracleDB
It is a common scenario where you do not design your database very well in the beginning of a project. Therefore you have to do some changes and alter tables. One common place for changes is the datatype of your column. You may need to change either its precision or the whole type. In most cases it is easy just 1 line of code :
ALTER TABLE table_name MODIFY column_name datatype
But that is not the case with changing VARCHAR2 to CLOB. The difference between the 2 types is that in VARCHAR2 you have to specify the length of the stored string and that length is limited to 4000 characters while CLOB can store up to 128 terabytes of character data in the database.
If you try to use the altered code from above you will get the following error:
ORA-22858: invalid alteration of datatype.
So we will use the following script to accomplish the task. It creates copy of the desired column which stores the current values, changes the type of the column and then writes back the data from the copy column.
ALTER TABLE REPORT ADD REPORT_DETAILS_COPY VARCHAR2(4000);
COMMIT;    -- Commit is here so we can reference the copy column later
UPDATE REPORT SET REPORT_DETAILS_COPY = REPORT_DETAILS;
COMMIT;
UPDATE REPORT SET REPORT_DETAILS = NULL;
COMMIT;
ALTER TABLE REPORT MODIFY REPORT_DETAILSLONG;
ALTER TABLE REPORT MODIFY REPORT_DETAILSCLOB;
UPDATE REPORT SET REPORT_DETAILS = REPORT_DETAILS_COPY ;
COMMIT;
ALTER TABLE REPORT DROP COLUMN REPORT_DETAILS_COPY ;
ALTER INDEX REPORT_PK REBUILD;

The last line is there because after the alternation of the table the index went in an unusable state. After the rebuild, everything works as expected.   Was this helpful? Do you have some other solutions that you can share? 

Categories

Denis Danov is the Chief Technology Officer at Dreamix and a thought leader in software architecture and technology leadership. With over 12 years at Dreamix, he has progressed from Junior Software Engineer to CTO, leading the company's technology and delivery teams to create scalable, maintainable custom software solutions for complex sectors like aviation and transportation. Denis completed the CTO Program at Wharton Executive Education, focusing on strategy, innovation frameworks, and organizational scaling. Specializing in Java and JavaScript, he designs business-driven, future-proof systems that support long-term growth and operational efficiency. He is passionate about knowledge sharing, having spoken at industry conferences such as Java2Days and contributed to leading tech publications including JAXenter and DZone. Denis is a thought leader and contributing writer, sharing insights on software architecture and building adaptable technology solutions.