• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5605
  • Last Modified:

Change column data type from VARCHAR2 to CLOB, retaining column order

Hi!

Is there a way to change the column order from varchar2 to clob in a table and still retain the columns order?

I would prefer to do this without using a temporary table as constraints and large amount of data may cause that solution to be time consuming and cumbersome.

Also, could you please list the 5-10 most important drawbacks of using clob as opposed to varchar2?
0
pcpaasche
Asked:
pcpaasche
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Ivo StoykovCommented:
Yes
  1. Create new column
  2. transfer from old to new column
  3. delete old column
This also could be made with the table
  1. Create new table
  2. transfer data from old to new table
  3. drop old table
hth
I
0
 
pcpaascheAuthor Commented:
Hi!

thank's for your proposal.

However, I am looking for a solution where:
1. Column order in the table doesn't change
2. No need to transfer data from the original table to a new table

Any other suggestions?
0
 
Ivo StoykovCommented:
Then you should use second proposal.
I doubt direct varchar2 to clob transformation is possible unless you have *only numbers* in that column.
I
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rbrookerCommented:
modifying a column to a clob is not possible, it results in a ORA-22858.  http://www.error-code.org.uk/view.asp?e=ORACLE-ORA-22858
you will need to either:
  • create a new table with the column of the right  type in the right place and copy the data, or
  • add a column on to the end of the existing table and copy the data from one column to the other
good luck.
0
 
pcpaascheAuthor Commented:
Or I can do like this:

1. Add a new temporary CLOB column at the end of the existing table
2. Copy data from the existing VARCHAR2-column to the new temporary CLOB-column: Update mytable set newClobColumn = existingVarchar2Column
3. Empty the existing VARCHAR2-column:  Update my table set VARCHAR2-column = NULL;
4. Change existing column from VARCHAR2 to LONG
5. Change existing column from LONG to CLOB  (cannot change directly from varchar2 to clob, must go via long)
6. Update existing column with values from temporary column: Update mytable set existingColumnNowClob = TemporaryColumnAtEndOf Table
7. Remove temporary column added to table in step 1

I just tested this solution and it works fine:-)

ALTER TABLE VARIABLELOGSTATEMENT ADD  VARIABLEVALUE_NEW CLOB;
Update VARIABLELOGSTATEMENT set VARIABLEVALUE_NEW = VARIABLEVALUE;
Update VARIABLELOGSTATEMENT set VARIABLEVALUE = null;
ALTER TABLE VARIABLELOGSTATEMENT modify VARIABLEVALUE long;
ALTER TABLE VARIABLELOGSTATEMENT modify VARIABLEVALUE clob;
Update VARIABLELOGSTATEMENT set VARIABLEVALUE = VARIABLEVALUE_NEW;
ALTER TABLE VARIABLELOGSTATEMENT drop column VARIABLEVALUE_NEW;

Open in new window

0
 
sdstuberCommented:
use an external table, save your data to the external table, then drop and recreate your table with the clob column and then use the external table to load the data.
you'll take extra space on your server but not in your database.

drawback to using a clob?  they are more complicated structures so they are a slower to work with.
depending on your version you may or may not be able to do things with them.  You posted to 9, 10 and 11 zones.

in 10 and 11, you can use clobs like "super varchar2" almost invisibly until you try to access more than 4000 characters in sql, or 32K in pl/sql
then you must use dbms_lob.


in 9, you can do a lot of things invisibly but you'll notice limitations when you try to use them across db links especially.
0
 
sdstuberCommented:
note, exporting to an external table is a 10g and above option.  So, for 9i, you'll have to save the data some otherway. then reimport
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now