http://bluefrog-oracle.blogspot.com/2011/09/script-submitted-to-otn-to-altter.html

Script to Alter varchar2 byte cols to char

 
 
The script below modifies all columns from VARCHAR2 BYTE to CHAR for all tables listed in the IN list. The script uses the USER_TAB_COLUMNS view. A log table has been created to record failures.

 
create table log_tbl (
table_name varchar2(30)
, column_name varchar2(30)
, msg varchar2(200)
, error_flag varchar2(1) default 'P') -- P for Pass and F for Fail.
/  SQL> select table_name, column_name, char_used
2 from user_tab_columns
3 where table_name in ('T1','T2')
4 / 
 
TABLE_NAME COLUMN_NAME C
------------------------------ ------------------------------ -
T1 A B
T2 A B
 
SQL> declare
2 l_Err varchar2(200);
3 begin
4 for r in (select atc.table_name, atc.column_name, atc.data_length
5 from user_tab_columns atc -- You would probably use ALL_
6 left outer join Log_Tbl lt on (atc.Table_name = lt.Table_Name
7 and atc.Column_name = lt.Column_Name
8 and lt.Error_Flag = 'P')
9 where atc.data_type = 'VARCHAR2'
10 and atc.char_used = 'B'
11 and atc.Table_Name in ('T1', 'T2', 'T3')) loop
12
13 begin
14 execute immediate 'alter table ' || r.table_name
15 || ' modify '
16 || r.column_name
17 || ' varchar2('
18 || r.data_length
19 || ' char)';
20
21 insert into Log_tbl (Table_Name, Column_Name)
22 values (r.Table_Name, r.Column_Name);
23
24 exception
25 when others then
26 l_Err := sqlerrm;
27 insert into Log_tbl (Table_Name, Column_Name, Msg, Error_Flag)
28 values (r.Table_Name, r.Column_Name, l_Err, 'F');
29 end;
30
31 commit;
32
33 end loop;
34
35 end;
36 / 
 
PL/SQL procedure successfully completed.
 
SQL> select table_name, column_name, char_used
2 from user_tab_columns
3 where table_name in ('T1','T2', 'T3')
4 / 
 
TABLE_NAME COLUMN_NAME C
------------------------------ ------------------------------ -
T1 A C
T2 A C
 
SQL> select table_name,column_name,error_flag
2 from log_tbl;
 
TABLE_NAME COLUMN_NAME E
--------------- --------------- -
T1 A P
T2 A P
 
SQL> create table t3 (a varchar2(20) )
2 / 
 
Table created.
 
SQL> insert into t3 (a) values ('Hello')
2 / 
 
1 row created.
 
SQL> select table_name, column_name, char_used
2 from user_tab_columns
3 where table_name in ('T1','T2', 'T3'); TABLE_NAME COLUMN_NAME C
------------------------------ ------------------------------ -
T1 A C
T2 A C
T3 A B 
 
Note the difference in the column char usage between T3 and the other tables given that T3 was created after the script was executed.
 
SQL> declare
2 l_Err varchar2(200);
3 begin
4 for r in (select atc.table_name, atc.column_name, atc.data_length
5 from user_tab_columns atc -- You would probably use ALL_
6 left outer join Log_Tbl lt on (atc.Table_name = lt.Table_Name
7 and atc.Column_name = lt.Column_Name
8 and lt.Error_Flag = 'P')
9 where atc.data_type = 'VARCHAR2'
10 and atc.char_used = 'B'
11 and atc.Table_Name in ('T1', 'T2', 'T3')) loop
12
13 begin
14 execute immediate 'alter table ' || r.table_name
15 || ' modify '
16 || r.column_name
17 || ' varchar2('
18 || r.data_length
19 || ' char)';
20
21 insert into Log_tbl (Table_Name, Column_Name)
22 values (r.Table_Name, r.Column_Name);
23
24 exception
25 when others then
26 l_Err := sqlerrm;
27 insert into Log_tbl (Table_Name, Column_Name, Msg, Error_Flag)
28 values (r.Table_Name, r.Column_Name, l_Err, 'F');
29 end;
30
31 commit;
32
33 end loop;
34
35 end;
36 / 
 
PL/SQL procedure successfully completed.
 
SQL> select table_name, column_name, char_used
2 from user_tab_columns
3 where table_name in ('T1','T2', 'T3')
4 / 
 
TABLE_NAME COLUMN_NAME C
--------------- --------------- -
T1 A C
T2 A C
T3 A C
 
The script uses the USER_TAB_COLUMNS view. If you modify the script to use the ALL_TAB_COLUMNS or the DBA_TAB_COLUMNS view, then the script would look as follows:
 
undefine schema_name
declare
l_Err varchar2(200);
begin
for r in (select atc.table_name, atc.column_name, atc.data_length
from all_tab_columns atc -- You would probably use ALL_
left outer join Log_Tbl lt on (atc.Table_name = lt.Table_Name
and atc.Column_name = lt.Column_Name
and lt.Error_Flag = 'P')
where atc.data_type = 'VARCHAR2'
and atc.char_used = 'B'
and atc.Table_Name in ('T1', 'T2', 'T3')
and atc.owner = upper('&&schema_name')) loop begin
execute immediate 'alter table '|| upper('&&schema_name')
|| '.'
|| r.table_name
|| ' modify '
|| r.column_name
|| ' varchar2('
|| r.data_length
|| ' char)'; insert into Log_tbl (Table_Name, Column_Name)
values (r.Table_Name, r.Column_Name); exception
when others then
l_Err := sqlerrm;
insert into Log_tbl (Table_Name, Column_Name, Msg, Error_Flag)
values (r.Table_Name, r.Column_Name, l_Err, 'F');
end; commit; end loop; end;
/
 
The default bhaviour, when the usage is not specified explicitly, is to set each column to BYTE. The default behaviour can be altered by setting NLS_LENGTH_SEMANTICS, for example:
 

SQL> ALTER SESSION SET NLS_LENGTH_SEMANTICS=BYTE;

Session altered.

SQL> DROP TABLE T1;

Table dropped.

SQL> DROP TABLE T2;

Table dropped.

SQL> ALTER SESSION SET NLS_LENGTH_SEMANTICS=CHAR;

Session altered.

SQL> create table t1 (a varchar2(10));

Table created.

SQL> select table_name,column_name,char_used from user_tab_columns
  2  where table_name = 'T1';

TABLE_NAME COLUMN_NAME C
---------- ----------- -
T1         A           C

SQL> ALTER SESSION SET NLS_LENGTH_SEMANTICS=BYTE;

Session altered.

SQL> create table t2 (a varchar2(10));

Table created.

SQL> select table_name,column_name,char_used from user_tab_columns
  2  where table_name IN ('T2', 'T1');

TABLE_NAME COLUMN_NAME C
---------- ----------- -
T1         A           C
T2         A           B

change all column to char的更多相关文章

  1. json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    1.json模块常用的四个函数 import json json.load() # 将一个存储在文件中的json对象(str)转化为相对应的python对象 json.loads() # 将一个jso ...

  2. ValueError: Expecting property name: line 1 column 1 (char 1)

    # -*- coding: cp936 -*- #xiaodeng #python 2.7.10 import weibo s='{"name":"xiaodeng&qu ...

  3. json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 column 33 (char 33)

    json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 colu ...

  4. 记录一个奇葩 bug [Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)]

    关于 flask 的一个记录 代码 @auth.login_required @app.route('/add', methods=['POST']) def add(): if request.me ...

  5. SQL-Server collation, what is it and how to change db/column collation

    The thing about collations is that although database have it's own collation, every table, and every ...

  6. 41-json.decoder.JSONDecodeError: Invalid control character at: line 6894 column 12 (char 186418)

    在使用python中将单词本的单词用正则匹配成字典后,以json存储,仪json读入,但是一直报错: 原因是: 正则处理后的数据有的出了点问题,导致一个字典的 有多个相同的键!!!,则肯定会报错啊!! ...

  7. Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 1 (char 1)

    解决办法,json数据只能用双引号,而不能用单引号

  8. python,json解析字符串时ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

    今天写测试工具的时候,去excel取数据,用json解析字符串为字典时报错,后经调试,发现是单引号的原因,将单引号换位双引号即可 def getExcelValue_to_dic(filepath): ...

  9. ValueError: Expecting property name: line 1 column 2 (char 1)

    代码: import json str2 = '{"domain":"456"}' str1 = "{'domain':'123'}" pr ...

随机推荐

  1. java 两个对象共使一个方法

  2. Center OS 7

    1:关闭防火墙 systemctl stop iptables.service 2:禁止开启启动 systemctl disable firewalld.service 3:查看防火墙 firewal ...

  3. instanceof是Java的一个二元操作符(运算符)

    instanceof是Java的一个二元操作符(运算符),也是Java的保留关键字.它的作用是判断其左边对象是否为其右边类的实例,返回的是boolean类型的数据.用它来判断某个对象是否是某个Clas ...

  4. gensim word2vec |来自渣渣硕的学习笔记

    最近写论文跑模型,要用到word2vec,但是发现自己怎么也看不懂网上的帖子,还是自己笨吧,所以就有了我的第一篇博客!!!  关于word2vec工具打算写一个系列的,当然今天这篇文章只打算写: 如何 ...

  5. C++ 概率算法 利用蒙特卡罗算法计算圆周率

    概率算法大致可分为4种形式: 数值概率算法: 蒙特卡罗算法: 拉斯维加斯算法: 舍伍德算法: 计算蒙特卡罗概率的算法实现: #include "stdio.h" #include ...

  6. list的过滤操作

    假设 l = ['abc', 'mn', 'aq', 'liuming'] 我要过滤出以a开头的元素,方法有以下两种 方法1: l = ['abc', 'mn', 'aq', 'liuming'] l ...

  7. FS,FT,DFS,DTFT,DFT,FFT的联系和区别 数字信号处理

    DCT变换的原理及算法 文库介绍 对于初学数字信号处理(DSP)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. 学习过<高等数学>和<信号与系统 ...

  8. 简单数学算法demo和窗口跳转,关闭,弹框

     简单数学算法demo和窗口跳转,关闭,弹框demo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&quo ...

  9. (转)Kubernetes设计架构

    转:https://www.kubernetes.org.cn/kubernetes设计架构 Kubernetes集群包含有节点代理kubelet和Master组件(APIs, scheduler, ...

  10. (转)docker-compose安装

    转:https://blog.csdn.net/pushiqiang/article/details/78682323 https://blog.csdn.net/ericnany/article/d ...