oracle 設定 大小寫 敏感

來源:互聯網
上載者:User

(以下內容只有在"="號的情況下生效,LIKE 不行。)

有時候我們查詢的時候不想通過lower或者upper等函數而把大小寫內容都查詢出來,如從sqlserver、mysql等轉移到
oracle時。在oracle10g可以通過修改session來實現:

ALTER SESSION SET NLS_COMP=ANSI;

ALTER SESSION SET NLS_SORT=binary_ci;

解析:

SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle
Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL
Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production

TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL
Version 10.2.0.1.0 - Production

SQL> select * from test;

ID
--------------------
A
a

SQL> select * from test where id='a';

ID
--------------------
a

SQL> ALTER SESSION SET NLS_COMP=ANSI;

Session altered

SQL> ALTER SESSION SET NLS_SORT=binary_ci;

Session altered

SQL> select * from test where id='A';

ID
--------------------
A
a

SQL> select * from test where id='a';

ID
--------------------
A
a

SQL>

10gR2中,nls_com新增加了一個值LINGUISTIC
,設定這個值,可以使在nl_sort中設定大小寫不敏感。相應的,nl_sort也增加了一個值:BINARY_CI,(CI即Case
Insensitive),也就是大小寫不敏感。

但是,實際上設定過這兩個值以後,並非真正大小寫不敏感了,而是相當於Oracle會自動給語句加上upper函數。看以下例子:

SQL> set autot on
SQL> select * from t2 where f1 = 'a';

F1 AAA
---------- ----------
a 2

Execution Plan
----------------------------------------------------------
Plan
hash value: 2238318762

--------------------------------------------------------------------------------
-------
|
Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Tim
e |
--------------------------------------------------------------------------------
-------
|
0 | SELECT STATEMENT | | 1 | 5 | 2 (0)| 00:
00:01 |
| 1 | TABLE
ACCESS BY INDEX ROWID| T2 | 1 | 5 | 2 (0)| 00:
00:01 |
|* 2 |
INDEX RANGE SCAN | T2_IDX1 | 1 | | 1 (0)| 00:
00:01 |
--------------------------------------------------------------------------------
-------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access("F1"='a')

Statistics
----------------------------------------------------------
0
recursive calls
0 db block gets
0 consistent gets
0 physical
reads
0 redo size
0 bytes sent via SQL*Net to client
0 bytes
received via SQL*Net from client
0 SQL*Net roundtrips to/from client
0
sorts (memory)
0 sorts (disk)
1 rows processed

SQL> alter session set NLS_SORT=BINARY_CI;

Session altered.

SQL> alter session set NLS_COMP=LINGUISTIC;

Session altered.

SQL> select * from t2 where f1 = 'a';

F1 AAA
---------- ----------
A 1
a 2

Execution Plan
----------------------------------------------------------
Plan
hash value: 1513984157

--------------------------------------------------------------------------
|
Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
|
0 | SELECT STATEMENT | | 1 | 5 | 3 (0)| 00:00:01 |
|* 1 | TABLE
ACCESS FULL| T2 | 1 | 5 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
----------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.