oracle like escape转义符的用法的介绍

oracle like escape转义符的用法的介绍


like 的匹配有两种模式.% and _ :

_  one charater.

% zero or more charaters.

下面是例子:

如果table_name 里面的名字里有'_' or '%' 你还想引用。 就加escape 语句:

表WM$CONSTRAINTS_TABLE和表PRODUCT_DESCRIPTIONS是用来做例子的,你也可以用自己取个带有特殊字符的表名,做实验

可以先查询一下,看看结果。对比一下,就知道like + escape 语句的用处了。
例子里已经带结果了。可以看看!!!!!!!!!!!!!!!

select table_name from all_tables where TABLE_NAME like 'W_$C%'; ----ok
select table_name from all_tables where TABLE_NAME like 'WM$C%'; ----ok
select table_name from all_tables where TABLE_NAME like 'W%$C%'; ----ok
select table_name from all_tables where TABLE_NAME like 'W%$%';  ----ok
WM$CONSTRAINTS_TABLE
---------
select table_name from all_tables where TABLE_NAME like 'P%__D%' ESCAPE '_'; ---ok---Attention--==> two underscore '_';

select table_name from all_tables where TABLE_NAME like 'P%_D%'; ---ok
select table_name from all_tables where TABLE_NAME like 'P%_D%S'; ----ok
select table_name from all_tables where TABLE_NAME like 'P%__D%' ESCAPE '_';   ====>PRODUCT_DESCRIPTIONS
select table_name from all_tables where TABLE_NAME like 'P%__R%' ESCAPE '_';
Note: if I don`t add the clause escape,the sytanx will give the underscore '_' two meanings.
 one is the underscore,other is one character .see the oracle documents Pattern-matching conditions.
 the sentence will give the resualt
 ===
 ====SQL> select table_name from all_tables where TABLE_NAME like 'P%_D%S';

  TABLE_NAME
  ------------------------------
  PRODUCT_DESCRIPTIONS
  PRODUCTS

 ========
This one is the resualt with the escape clause

  ===SQL> select table_name from all_tables where TABLE_NAME like 'P%__D%' ESCAPE '_';

(select table_name from all_tables where TABLE_NAME like 'P%\_D%' ESCAPE '\';)这句和上边的一样。怕同志们弄混了。esccape 定义的 escp_chare 后的特殊字符还原成原来的意义。

 Any character can follow ESCAPE except percent (%) and underbar (_).  这句是oralce原文档里的。这里比较晕  A wildcard character is treated as a literal if preceded by the escape character.

SELECT last_name
   FROM employees
   WHERE last_name
   LIKE '%A\_B%' ESCAPE '\';

这个是oracle 给的例子。但是我的例子里 用'_'同样可以得到一样的结果。

还就是在 利用索引的时候,会快一些。如果是大表的话 非常之好。但是like condition 不要用%开头,这样还是扫描原表,不走索引的。

====

   TABLE_NAME
  ------------------------------
  PROXY_DATA$
  PROXY_ROLE_DATA$
  PRODUCT_DESCRIPTIONS

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/203348/viewspace-1399770/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/203348/viewspace-1399770/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值