Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace CHAR with VARCHAR2

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.

like image 785
Aaron Digulla Avatar asked Feb 25 '26 22:02

Aaron Digulla


1 Answers

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
);
like image 75
Jeffrey Kemp Avatar answered Feb 28 '26 15:02

Jeffrey Kemp