Friday, August 26, 2011

Converting to AL32UTF8 Character Set from the Oracle Default of WE8MSWIN1252

The situation:
  1. Current database uses WE8MSWIN1252
  2. Migrating to new database, considering going to AL32UTF8 encoding.
  3. AL32UTF8 is an Oracle UTF8 encoding of the Unicode characater set. Hence it is multibyte.  WE8MSWIN1252 is a single byte character set.
  4. I want to use expdp/impdp to handle character set migration
What can go wrong? 
  1. A varchar2 field declared with a max length of 4000 that contains 4000 bytes, or close to it, and contains characters that are converted to multibyte representations on import will fail with an ORA-12899.  This is a complaint that the maximum length for the column has been exceeded.  Why? Remember VARCHAR2 fields are constrained to 4000 bytes, not 4000 characters.  Need proof? Create a sample database using the AL32UTF8 character set and try and declare a VARCHAR2(4000) column.  It will fail, regardless of whether or not you use character/byte level semantics when declaring the column ala VARCHAR2(4000 CHAR).
    • How To Fix:
      • Convert any column you perceive as close to its 4000 character limit to a CLOB.
      • You will need to precreate your tables in the schema, how?
        • imdp user/pass dumpfile=test.dmp content=metadata_only data_options=skip_constraint_errors schemas=your-schema
      • After precreating your schema, drop your offending varchar2 columns and then replace with CLOB columns.
        • alter table my-table add(clob_column_name CLOB DEFAULT EMPTY_CLOB() NOT NULL);
      • Run another data import
        • Do yourself a favor and disable constraints and drop indexes before running the import below.  
        • imdp user/pass dumpfile=test.dmp data_options=skip_constraint_errors table_exists_action=append schemas=your-schema content=data_only
In a nutshell:
  1. Export Schema
  2. Precreate tables
  3. Drop offending varchar2 columns
  4. Replace Dropped columns with CLOBs
  5. Do data only import.
  6. Test your application for any other issues.

0 comments: