Home » SQL & PL/SQL » SQL & PL/SQL » Re: Need help converting column datatype and maintaining data
Re: Need help converting column datatype and maintaining data [message #1966] Thu, 13 June 2002 11:53 Go to next message
Orakin
Messages: 3
Registered: June 2002
Junior Member
Can I just create a new column in the existing column with a varchar2 datatype and then copy the data of the long column into the new varchar2 column...then drop the long column and rename the varchar2 column???

Was that complicated?
Re: Need help converting column datatype and maintaining data [message #1971 is a reply to message #1966] Thu, 13 June 2002 22:20 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Hi, here's one way to do it:

1. Create a table similar to the original but replace the LONG datatype by a VARCHAR2 datatype.
2. Verify that the LONG column data doesn't exceed 4000 bytes.
3. Create an anonymous pl/sql block similar to the one below, and execute it.
TABLES:
SQL> desc test_1
 Name                            Null?    Type
 ------------------------------- -------- ----
 COL_1                                    NUMBER
 COL_2                                    LONG

SQL> desc test_2
 Name                            Null?    Type
 ------------------------------- -------- ----
 COL_1                                    NUMBER
 COL_2                                    VARCHAR2(4000)

PL/SQL Block:

declare
   string varchar2(4000);
   v_id number;
   cursor c_test_1
       is
   select col_1, col_2
     from test_1;
begin
  for r_test_1 in c_test_1 loop
   insert into test_2 values (r_test_1.col_1, r_test_1.col_2);
  end loop;
  commit;
end;
/

4. remove the original table and rename the new one to the name of the original table. Verify that all triggers and constraints are set properly.

HTH,
MHE
Previous Topic: ERROR: ORA-01555 Snapshot too old
Next Topic: Varchar2 and Char - Urgent
Goto Forum:
  


Current Time: Thu Apr 25 09:32:45 CDT 2024