3

How can I replace CHAR with VARCHAR2 in all tables in a schema?

Note: I'm content with a query that returns the ALTER TABLE statements so I can save the script and run it again.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 2
    One thing to be aware of; you may have trailing spaces in your new `VARCHAR2` columns, and Oracle does consider trailing spaces to be significant in `VARCHAR2` columns, so the values `'A'` and `'A '` are not equivalent. – Adam Musch Mar 15 '10 at 15:03

1 Answers1

6
select 'ALTER TABLE "' || owner || '"."' || table_name
|| '" MODIFY ("' || column_name
|| '" VARCHAR2(' || data_length || '));'
from all_tab_columns tc
where data_type = 'CHAR'
and owner = :schemaname
and exists (
    select 1
    from all_tables t
    where tc.owner = t.owner
    and tc.table_name = t.table_name
);
msmucker0527
  • 5,164
  • 2
  • 22
  • 36
Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
  • 1
    I've extended the original answer to omit tables which are in the recycle bin. See http://stackoverflow.com/questions/2446053/what-are-the-bin-tables-in-oracles-all-tab-columns-table – Aaron Digulla Mar 16 '10 at 09:50