Solved

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

Posted on 2008-10-07
7
5,577 Views
Last Modified: 2013-12-19
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
Comment
Question by:pcpaasche
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 22658932
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
 

Author Comment

by:pcpaasche
ID: 22659112
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
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 22659310
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 18

Expert Comment

by:rbrooker
ID: 22665600
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
 

Accepted Solution

by:
pcpaasche earned 0 total points
ID: 22666977
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
 
LVL 73

Expert Comment

by:sdstuber
ID: 22683053
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
 
LVL 73

Expert Comment

by:sdstuber
ID: 22683062
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to recover a database from a user managed backup

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now