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. sql中强制使用索引

    如果在sql中使用的索引不生效,可以使用FORCE INDEX(索引),来强制使用索引: 例: SELECT COUNT(DEAL_STAT) FROM  c_pac FORCE INDEX (IDX ...

  2. js插件-图片椭圆轮播效果

    插件效果图: html 代码如下: <div id="container"> <img src="images/cartoon/1.jpg" ...

  3. Apache Flink 进阶(六):Flink 作业执行深度解析

    本文根据 Apache Flink 系列直播课程整理而成,由 Apache Flink Contributor.网易云音乐实时计算平台研发工程师岳猛分享.主要分享内容为 Flink Job 执行作业的 ...

  4. Windows系统启动iis方法详解

    很多网友一般都用Windows 系统自带的iis服务器来配置web网站,在本地进行调试和修改后才正式上线.虽说操作不难,但是小白来说却无从下手,很多人根本不知道iss在哪,怎么启动,更谈不上配置或者其 ...

  5. mysql8.0 新特性,对json类型的常用操作

    mysql8 新特性-json数据类型操作 -- 根据key(可多个)获取value SELECT JSON_EXTRACT('{"id": 14, "name" ...

  6. 基础复习之HTML (meta标签、块级元素与行内元素)

    一.meta标签 SEO 如何在不使用JS的情况下刷新页面(http-equiv="refresh" , content="time") 设置页面缓存 移动端设 ...

  7. 测开之路五十七:实现runner和测试报告

    准备测试用例 from fox.case import Casefrom src import Calculator class TestCalculator(Case): def setUp(sel ...

  8. iview+vue 表格中添加图片

    开门见山,话不多说,要在表格中添加图片,可以使用td: <table " width="100%"> <tr class="tr-style ...

  9. mockito测试入门学习

    一.什么是mock测试,什么是mock对象? 先来看看下面这个示例: 从上图可以看出如果我们要对A进行测试,那么就要先把整个依赖树构建出来,也就是BCDE的实例. 一种替代方案就是使用mocks 从图 ...

  10. 【原创】复制知乎“禁止转载”的内容做笔记 - 基于oncopy监听器的简单解决方案

    原理:移除所有oncopy的监听器. 使用: 新建书签,地址设为: javascript: getEventListeners(document).copy.forEach(({listener}) ...