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. H2database创建表

    语法和sql server大同小异 create table users(id int primary key not null int identity, name varchar(20))

  2. Es学习第八课, Filter、bool和范围查询

    Filter过滤查询 filter是不计算相关性的,同时可以缓存.因此filter速度快于query. 我们先在kibana上先添加数据来做准备 POST /lib4/items/_bulk { &q ...

  3. AGC003[BCDEF]题解

    2018-12-28 有点累EF明天再写叭=v= 2018-12-29 update EF B - Simplified mahjong 可以注意到 一段连续的非0序列都可以凑出 就是显然%2=0的可 ...

  4. js-ifelse-奇技淫巧

    我们有A,B,C,D四个不同的类别,在最开始的时候只有三个类别,并且两个类别是做同样的事: function categoryHandle(category) { if(category !== 'A ...

  5. python使用HTMLTestRunner.py生成测试报告

    这里我使用的是python selenium webdriver环境,浏览器驱动安装见selenium 1.下载HTMLTestRunner.py:http://tungwaiyip.info/sof ...

  6. Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

    问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

  7. opencc模块用langconv替换

    将一下两个py下载并放入代码目录:https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py https: ...

  8. [CSP-S模拟测试]:画作(BFS+数学)

    题目描述 小$G$的喜欢作画,尤其喜欢仅使用黑白两色作画.画作可以抽象成一个$r\times c$大小的$01$矩阵.现在小$G$构思好了了他的画作,准备动笔开始作画.初始时画布是全白的,他每一次下笔 ...

  9. 控制透明度(兼容IE FF)

    filter:alpha(opacity=70);opacity:0.7;

  10. python中的_ElementUnicodeResult是什么

    _ElementUnicodeResult在python中是字符串的一种,因为在python3中,字符串就是指以unicode编码规则存储的数据,而以其他方式如utf-8,ASCII编码方式存储的数据 ...