NLS 参数中跟数据库字符集有关的参数 NLS_CHARACTER 和 NLS_NCHAR_CHARACTER 在租户创建好后就不能修改,目前仅支持修改跟时间和日期格式相关的 NLS 参数。
修改 NLS 参数分为修改数据库级别的 NLS 参数和修改会话级别的 NLS 参数,前者需要租户的管理员权限用户(默认用户 sys)才可以。
修改数据库级别的 NLS 参数
修改一个数据库级别的 NLS 参数通常是在所属的 Oracle 租户内部,有两种方法:
- 通过 ALTER SYSTEM SET 命令修改数据库级别 NLS 参数。
- 通过 SET GLOBAL 命令修改数据库级别 NLS 参数。
示例:修改数据库级别的 NLS 参数
obclient> select * from nls_database_parameters where parameter in ('NLS_DATE_FORMAT','NLS_TIMESTAMP_FORMAT','NLS_TIMESTAMP_TZ_FORMAT');
+-------------------------+------------------------------+
| PARAMETER | VALUE |
+-------------------------+------------------------------+
| NLS_DATE_FORMAT | DD-MON-RR |
| NLS_TIMESTAMP_FORMAT | DD-MON-RR HH.MI.SSXFF AM |
| NLS_TIMESTAMP_TZ_FORMAT | DD-MON-RR HH.MI.SSXFF AM TZR |
+-------------------------+------------------------------+
3 rows in set (0.00 sec)
obclient> alter system set nls_date_format='YYYY-MM-DD';
Query OK, 0 rows affected (0.01 sec)
obclient> set global nls_timestamp_format='YYYY-MM-DD HH24:MI:SSXFF';
Query OK, 0 rows affected (0.01 sec)
obclient> set global nls_timestamp_tz_format='YYYY-MM-DD HH24:MI:SSXFF';
Query OK, 0 rows affected (0.01 sec)
obclient> select * from nls_database_parameters where parameter in ('NLS_DATE_FORMAT','NLS_TIMESTAMP_FORMAT','NLS_TIMESTAMP_TZ_FORMAT');
+-------------------------+--------------------------+
| PARAMETER | VALUE |
+-------------------------+--------------------------+
| NLS_DATE_FORMAT | YYYY-MM-DD |
| NLS_TIMESTAMP_FORMAT | YYYY-MM-DD HH24:MI:SSXFF |
| NLS_TIMESTAMP_TZ_FORMAT | YYYY-MM-DD HH24:MI:SSXFF |
+-------------------------+--------------------------+
3 rows in set (0.00 sec)
obclient> select * from nls_session_parameters where parameter in ('NLS_DATE_FORMAT','NLS_TIMESTAMP_FORMAT','NLS_TIMESTAMP_TZ_FORMAT');
+-------------------------+------------------------------+
| PARAMETER | VALUE |
+-------------------------+------------------------------+
| NLS_DATE_FORMAT | DD-MON-RR |
| NLS_TIMESTAMP_FORMAT | DD-MON-RR HH.MI.SSXFF AM |
| NLS_TIMESTAMP_TZ_FORMAT | DD-MON-RR HH.MI.SSXFF AM TZR |
+-------------------------+------------------------------+
3 rows in set (0.00 sec)
从上面示例可以看出,修改数据库级别的 NLS 参数并不会应用于当前会话,需要断开会话重新连接才会生效。
obclient> select sysdate, systimestamp from dual;
+-----------+-------------------------------------+
| SYSDATE | SYSTIMESTAMP |
+-----------+-------------------------------------+
| 02-APR-20 | 02-APR-20 07.50.58.427570 PM +08:00 |
+-----------+-------------------------------------+
1 row in set (0.00 sec)
obclient> show session variables where variable_name in ('nls_date_format','nls_timestamp_format','nls_timestamp_tz_format');
+-------------------------+------------------------------+
| VARIABLE_NAME | VALUE |
+-------------------------+------------------------------+
| nls_date_format | DD-MON-RR |
| nls_timestamp_format | DD-MON-RR HH.MI.SSXFF AM |
| nls_timestamp_tz_format | DD-MON-RR HH.MI.SSXFF AM TZR |
+-------------------------+------------------------------+
3 rows in set (0.01 sec)
obclient>
修改会话级别的 NLS 参数
客户端会话可以设置跟数据库不同的 NLS 参数,当时只支持对日期和时间格式的 NLS 参数进行修改。
会话级别的 NLS 参数有两种修改方法:
- 通过 ALTER SESSION SET 命令修改
- 通过 SET [SESSION] VARIABLE 命令修改
示例:修改会话级别的 NLS 参数
obclient> alter session set nls_date_format='YYYY/MM/DD';
Query OK, 0 rows affected (0.00 sec)
obclient> set session nls_timestamp_format='YYYY/MM/DD HH.MI.SSXFF AM';
Query OK, 0 rows affected (0.00 sec)
obclient> set session nls_timestamp_tz_format='YYYY/MM/DD HH.MI.SSXFF AM TZR';
Query OK, 0 rows affected (0.00 sec)
obclient> select sysdate, systimestamp from dual;
+------------+--------------------------------------+
| SYSDATE | SYSTIMESTAMP |
+------------+--------------------------------------+
| 2020/04/02 | 2020/04/02 07.51.52.254705 PM +08:00 |
+------------+--------------------------------------+
1 row in set (0.00 sec)
obclient> show variables where variable_name in ('nls_date_format','nls_timestamp_format','nls_timestamp_tz_format');
+-------------------------+-------------------------------+
| VARIABLE_NAME | VALUE |
+-------------------------+-------------------------------+
| nls_date_format | YYYY/MM/DD |
| nls_timestamp_format | YYYY/MM/DD HH.MI.SSXFF AM |
| nls_timestamp_tz_format | YYYY/MM/DD HH.MI.SSXFF AM TZR |
+-------------------------+-------------------------------+
3 rows in set (0.01 sec)
obclient>
会话级别的 NLS 参数修改作用范围是当前会话,如果会话断开再重连,又会获取数据库级别的 NLS 参数作为会话参数的初始值。