Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change all column units from byte to char

I've been given a task to convert a huge database in MS SQL Server to Oracle. The database has more than 150 tables with lots of constraints,defaults and relationships, that many stored procedures and triggers. Now I wonder 2 things:

  1. How is this generally done? Do we have to do it manually for every object one by one or there's some better way?
  2. String columns in Oracle are by default created in byte units. How can I update all the columns in all the tables from byte to char?

Thanks in advance.

like image 726
Mikayil Abdullayev Avatar asked Nov 16 '11 14:11

Mikayil Abdullayev


People also ask

What is the difference between CHAR and byte in Oracle?

The CHAR data type can store a character string with the size from 1 to 2000 bytes. If you don't explicitly specify BYTE or CHAR followed the length , Oracle uses the BYTE by default. When you insert or update a fixed-length character string column, Oracle stores the characters as the fixed-length data.

What is the difference between VARCHAR2 byte and VARCHAR2 CHAR?

Varchar2(10) uses the current value of NLS_LENGTH_SEMANTICS to determine the limit for the string. If this is byte, then it's 10 bytes. If it's char, then it's 10 characters. In multibyte character sets these can be different!

Is CHAR bigger than byte?

3) Another difference between char and byte is that char is a larger data type than a byte. The range of byte is between -128 to 127 but the range of char is from 0 to 65535 because a byte is a signed 8-bit data type and char is an unsigned 16-bit data type hence, its maximum value is 2 ^ 16 - 1 which is 65535.

What does VARCHAR2 20 byte mean?

VARCHAR2(20 BYTE) : Allows only the specified number of bytes to be stored in the column, regardless of how many characters this represents. VARCHAR2(20 CHAR) : Allows the specified number of characters to be stored in the column regardless of the number of bytes this equates to.


2 Answers

For 2: This will produce code to update all BYTE columns to CHAR

BEGIN
    FOR x in (SELECT * FROM user_tab_columns WHERE data_type LIKE 'VARCHAR%' and CHAR_USED = 'B')
    LOOP
        DBMS_OUTPUT.PUT_LINE('ALTER TABLE '||x.table_name||' MODIFY '||x.column_name||' '||x.data_type||'('||x.data_length||' CHAR)');
    END LOOP;
END;
/
like image 135
grokster Avatar answered Nov 13 '22 03:11

grokster


I migrated MS SQL Server database to Informix with self made batch that used ADO.NET metadata to retrieve db structure. I did that because I couldn't find appropriate tool for this conversion. I wouldn't advise this approach with databases that can be migrated using exisitng tool.
So, in case of migrating to Oracle it is better to use some of existing tools like this:

http://www.oracle.com/technetwork/database/migration/sqlserver-095136.html

like image 20
Filip Avatar answered Nov 13 '22 01:11

Filip