12C单实例测试,varchar2在早期版本中最大长度限制为4000,12C版本可以通过修改参数MAX_STRING_SIZE将最大长度限制调整为32767。
早期版本中虽然SQL数据类型限制为4000(如表中的列的varchar2类型),但在PL/SQL中的限制是32767(如在存储过程中的定义变量时用的varchar2类型)。
1,需要在UPGRADE模式下修改参数和跑脚本utl32k.sql。
Use ALTER SYSTEM only when the database is in UPGRADE mode, and run the utl32k.sql script afterward, as explained in this section.
2,参数MAX_STRING_SIZE只控制 VARCHAR2, NVARCHAR2, and RAW这三个数据类型的最大值。
MAX_STRING_SIZE controls the maximum size of VARCHAR2, NVARCHAR2, and RAW data types in SQL.
3,STANDARD模式下,VARCHAR2 and NVARCHAR2最大值为4000,RAW最大为2000。
STANDARD means that the length limits for Oracle Database releases prior to Oracle Database 12c apply (for example, 4000 bytes for VARCHAR2 and NVARCHAR2, and 2000 bytes for RAW).
4,EXTENDED模式下,上述三个数据类型的最大值为32767。
EXTENDED means that the 32767 byte limit introduced in Oracle Database 12c applies.
5,COMPATIBLE这个兼容参数的版本为12.0.0.0及以上。
The COMPATIBLE initialization parameter must be set to 12.0.0.0 or higher to set MAX_STRING_SIZE = EXTENDED.
6,参数MAX_STRING_SIZE的值只能从STANDARD 修改为 EXTENDED,而不能从EXTENDED 修改为 STANDARD。
You can change the value of MAX_STRING_SIZE from STANDARD to EXTENDED. However, you cannot change the value of MAX_STRING_SIZE from EXTENDED to STANDARD.
Increasing the Maximum Size of VARCHAR2, NVARCHAR2, and RAW Columns in a Non-CDB
To increase the maximum size of VARCHAR2
, NVARCHAR2
, and RAW
columns in a non-CDB:
1.Shut down the database.
2.Restart the database in UPGRADE
mode.
3.Change the setting of MAX_STRING_SIZE
to EXTENDED
.
4.Run the rdbms/admin/utl32k.sql
script. You must be connected AS SYSDBA
to run the script.
5.Restart the database in NORMAL
mode.
Note:
The utl32k.sql
script increases the maximum size of the VARCHAR2
, NVARCHAR2
, and RAW
columns for the views where this is required. The script does not increase the maximum size of the VARCHAR2
, NVARCHAR2
, and RAW
columns in some views because of the way the SQL for those views is written.
6.Run the rdbms/admin/utlrp.sql
script to recompile invalid objects. You must be connected AS SYSDBA
to run the script.
1,以sysdba连接数据库
[oracle]$ sqlplus / as sysdba
SQL> show parameter COMPATIBLE ; --兼容性参数compatible要为12.0.0.0.0及以上
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
compatible string 12.1.0.2.0
SQL> show parameter MAX_STRING_SIZE; --字符串大小参数max_string_size初始值为STANDARD
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
max_string_size string STANDARD
2,关闭数据库,并以upgrade试启动数据库
SQL> shutdown immediate;
SQL> startup upgrade;
3,修改参数
SQL> alter system set max_string_size=extended scope=both;
4,字符类型扩展脚本
SQL> @?/rdbms/admin/utl32k.sql
SQL> select count(*) from dba_objects where status<>'VALID'; --查看invalid状态
COUNT(*)
----------
3259
5,以NOMAL模式重启数据库
SQL> shutdown immediate;
SQL> startup;
6,执行脚本编译数据库
SQL> @?/rdbms/admin/utlrp.sql
SQL> select count(*) from dba_objects where status<>'VALID';
COUNT(*)
----------
0
说明:
上面代码测试过程中,执行完扩展脚本后多次重启失败,报错记录如下 :
第一次,执行完扩展脚本无法正常重启,此时是有3259个invalid的,打算重启后再执行编译脚本,重启报错如下
ORA-01092: ORACLE instance terminated. Disconnection forced
ORA-14694: database must in UPGRADE mode to begin MAX_STRING_SIZE migration
第二次,重启失败后再次重启
ORA-01092: ORACLE instance terminated. Disconnection forced
ORA-14695: MAX_STRING_SIZE migration is incomplete
第三次,以upgrade模式重启,启动后两次执行编译脚本utlrp.sql,invalid降为0;再次以NOMAL模式重启,报错如下
ORA-01092: ORACLE instance terminated. Disconnection forced
ORA-14695: MAX_STRING_SIZE migration is incomplete
第四次,以upgrade模式重启,启动后执行扩展脚本utl32k.sql,再执行编译脚本utlrp.sql,invalid降为0;最后以NOMAL模式重启,正常。
补:报错分析,修改参数max_string_size=extended时用的scope=spfile,然后再跑的扩展脚本,怀疑就是因为此时内存里参数值仍为stardard,跑扩展脚本是不生效的。要重启DB使max_string_size=extended生效后再跑扩展脚本就可以了。避免上述报错的办法是修改参数max_string_size=extended时用scope=both。
字符长度扩展后,测试使用:
SQL> create table test1(name varchar2(30000));
Table created.
SQL> insert into test1 values('kiti');
1 row created.