nls_date_format参数设置的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dream_ling/article/details/81903009

今天在练习TO_DATE()和TO_CHAR()函数的时候发现了一个问题,就是说nls_date_format参数在会话级alter session修改起作用,可在系统级alter system却不起作用.具体情况如下:

[php] view plain copy

  1. SQL> select sysdate from dual;  
  2.   
  3. SYSDATE  
  4. ---------------  
  5. 19-OCT-09  
  6. SQL> alter session set nls_date_format="MON-DD-YYYY";  
  7.   
  8. Session altered.  
  9. SQL> select sysdate from dual;  
  10.   
  11. SYSDATE  
  12. -----------------  
  13. OCT-19-2009  
  14.   
  15. SQL>  
  16. ========================================================  
  17. SQL> alter system set nls_date_format="MON-DD-YYYY" scope=spfile;  
  18.   
  19. System altered.  
  20.   
  21. SQL> shutdown immediate  
  22. Database closed.  
  23. Database dismounted.  
  24. ORACLE instance shut down.  
  25. SQL> startup  
  26. ORACLE instance started.  
  27.   
  28. Total System Global Area  176160768 bytes  
  29. Fixed Size                  1247948 bytes  
  30. Variable Size              92276020 bytes  
  31. Database Buffers           79691776 bytes  
  32. Redo Buffers                2945024 bytes  
  33. Database mounted.  
  34. Database opened.  
  35. SQL> show parameter date  
  36. NAME                                 TYPE        VALUE  
  37. ------------------------------------ ----------- ------------------------------  
  38. fixed_date                           string  
  39. nls_date_format                      string      MON-DD-YYYY  
  40. nls_date_language                    string  
  41.   
  42. SQL> select sysdate from dual;  
  43.   
  44. SYSDATE  
  45. ---------------  
  46. 19-OCT-09  
  47.   
  48. SQL>  
  49. 这个时候没有显示OCT-19-2009 而却是19-OCT-09  

上网去搜了搜,找到了答案,那就是优先级的关系导致了这个问题.

系统级alter system的优先级低于会话级alter session的优先级,更加详细的情况接下来会演示出来.

[php] view plain copy

  1. nls参数、环境变量以及函数起作用的顺序如下(由低到高)  
  2. instance级参数--->nls_lang--->session级参数--->函数  
  3.   
  4.   
  5. C:>set nls_lang=SIMPLIFIED CHINESE_CHINA.ZHS16GBK  
  6. C:>sqlplus / as sysdba  
  7. SQL*Plus: Release 10.2.0.1.0 - Production on 星期三 7月 29 21:01:38 2009  
  8. Copyright (c) 1982, 2005, Oracle. All rights reserved.  
  9.   
  10. 连接到:  
  11. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  12. With the Partitioning, OLAP and Data Mining options  
  13. SQL> show parameter nls_date_format  
  14. NAME TYPE VALUE  
  15. ------------------------------------ ----------- ------------------------------  
  16. nls_date_format string yyyy/mm/dd hh24:mi:ss  
  17. --很明显实例级别参数nls_date_format没有启作用  
  18. SQL> select sysdate from dual;  
  19. SYSDATE  
  20. --------------  
  21. 29-7月 -09  
  22. --==============================  
  23. --在注册表中清除nls_lang的值,这是我们发现实例级参数nls_date_format:格式  
  24. yyyy/mm/dd hh24:mi:ss开始发挥作用  
  25. SQL> exit  
  26. 从 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  27. With the Partitioning, OLAP and Data Mining options 断开  
  28. C:>sqlplus / as sysdba  
  29. SQL*Plus: Release 10.2.0.1.0 - Production on Wed Jul 29 20:54:59 2009  
  30. Copyright (c) 1982, 2005, Oracle. All rights reserved.  
  31.   
  32. ???:  
  33. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  34. With the Partitioning, OLAP and Data Mining options  
  35. SQL> select sysdate from dual;  
  36. SYSDATE  
  37. -------------------  
  38. 2009/07/29 20:55:01  
  39. --====================================  
  40. --恢复注册表中的nls_lang设置:SIMPLIFIED CHINESE_CHINA.ZHS16GBK  
  41. SQL> exit  
  42. ? Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  43. With the Partitioning, OLAP and Data Mining options ??  
  44. C:>sqlplus / as sysdba  
  45. SQL*Plus: Release 10.2.0.1.0 - Production on 星期三 7月 29 20:55:21 2009  
  46. Copyright (c) 1982, 2005, Oracle. All rights reserved.  
  47.   
  48. 连接到:  
  49. Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production  
  50. With the Partitioning, OLAP and Data Mining options  
  51. SQL> select sysdate from dual;  
  52. SYSDATE  
  53. --------------  
  54. 29-7月 -09  
  55. SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';  
  56. 会话已更改。  
  57. --显然session级别的nls_date_format发挥了作用  
  58. SQL> select sysdate from dual;  
  59. SYSDATE  
  60. -------------------  
  61. 2009-07-29 20:55:55  
  62. --很显然函数发挥了作用  
  63. SQL> select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;  
  64. TO_CHAR(SYSDATE,'YY  
  65. -------------------  
  66. 2009-07-29 09:10:01  
  67. SQL>  

这下就清楚了,呵呵.

猜你喜欢

转载自blog.csdn.net/Dream_ling/article/details/81903009