为单个查询更改 NLS_SORT

Change NLS_SORT for a single query

在通过脚本连续触发的大量查询中,单个查询使用 REGEXP_LIKE。在其中,我不希望正则表达式 ([a-z]) 匹配变音符号 (á、õ、ì)。我发现这样做的唯一方法是将 NLS_SORT 设置为 BINARY。但是,此查询不应影响之后的任何其他查询 运行。

因此,我只想为这个查询设置变量 NLS_SORT。但是,我宁愿不求助于 PL/SQL。有什么办法可以实现吗?

我更喜欢这个,但根据我找到的文档,不存在这样的参数:

SELECT * FROM dual WHERE REGEXP_LIKE('ë', '[a-z]', BINARY);

我可以想象这样的事情:

SELECT * FROM dual WHERE REGEXP_LIKE('ë', '[a-z]'); -- match
ALTER SESSION SET NLS_SORT = BINARY;
SELECT * FROM dual WHERE REGEXP_LIKE('ë', '[a-z]'); -- no match
ALTER SESSION SET NLS_SORT = DEFAULT; -- not working
ALTER SESSION RESET NLS_SORT;         -- not working

好吧,如果您知道哪些 carachtes 不匹配,您可以在另一个正则表达式中隐式排除它们:

SELECT * FROM dual WHERE REGEXP_LIKE('ë', '[a-z]') AND NOT REGEXP_LIKE('ë', '[ë]')

因此,感谢 Alex Poole,我重新评估了 REGEXP_LIKE 中的 match_paramater 选项。来自 documentation:

match_parameter is a text literal that lets you change the default matching behavior of the function. You can specify one or more of the following values for match_parameter:

  • ...
  • 'c' specifies case-sensitive matching.
  • ...

尽管区分大小写似乎不会影响 a 匹配 á 还是 e 匹配 ë,here 说明如果排序区分大小写,它也区分重音:

As of Oracle Database 10g, Oracle Database provides case-insensitive and accent-insensitive options for linguistic sorts. It provides the following types of monolingual and multilingual linguistic sorts:

  • Linguistic sorts that use information about base letters, diacritics, punctuation, and case. These are the standard monolingual and multilingual linguistic sorts that are described in "Using Linguistic Sorts".
  • Monolingual sorts that use information about base letters, diacritics, and punctuation, but not case, and multilingual sorts that use information about base letters and diacritics, but not case nor punctuation. This type of sort is called case-insensitive.
  • Monolingual sorts that use information about base letters and punctuation only and multilingual sorts that use information about base letters only. This type of sort is called accent-insensitive. (Accent is another word for diacritic.) Like case-insensitive sorts, an accent-insensitive sort does not use information about case.

这表明所有不区分重音的排序都是不区分大小写的,因此隐含地说明区分大小写的排序必须区分重音。

所以,总而言之:

  • 是的,有一个更好的方法来排除变音符号:SELECT * FROM dual WHERE REGEXP_LIKE('ë', '[a-z]', 'c');
  • 不,在不知道或不保存初始值的情况下,无法为单个查询更改 NLS_SORT 并还原它。