日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Oracle中使用escape關(guān)鍵字實(shí)現(xiàn)like匹配特殊字符,以及&字符的轉(zhuǎn)義

 liuyang_inf 2014-03-20
問(wèn)題描述:如果在一個(gè)表中的一個(gè)字段上存在'&',  '_',  '%'這樣的特殊字符,而我們又得在where條件中使用到這些特殊字符怎么辦?


1.創(chuàng)建含有這些特殊字符的表 

SQL> create table t_test_escape(name varchar2(20));
 
Table created
 
SQL> insert into t_test_escape(name) values('&_hello');
 
1 row inserted

--這里會(huì)提示我輸入變量_hello的值,我沒(méi)有輸入任何值,所以為空!
 
SQL> select * from t_test_escape;
 
NAME
--------------------

結(jié)果自然也為空
 
SQL> truncate table t_test_escape;
 
Table truncated

--使用set define off 關(guān)閉替代變量的功能
SQL> set define off
SQL> insert into t_test_escape(name) values('&_hello');
 
1 row inserted
 
SQL> insert into t_test_escape(name) values('%%_hello');
 
1 row inserted
 
SQL> insert into t_test_escape(name) values('Oracle%&_hello');
 
1 row inserted
 
SQL> commit;
 
Commit complete
 
SQL> select * from t_test_escape;
 
NAME
--------------------
&_hello
%%_hello
Oracle%&_hello

--使用set define off關(guān)閉變量替換功能之后,果然能夠插入含有&的特殊字符了。


2.使用escape關(guān)鍵字在模糊查詢中查看字符中含有%的字符串 
SQL> select * from t_test_escape where name like '%a%%' escape 'a';
 
NAME
--------------------
%%_hello
Oracle%&_hello

--上面使用的轉(zhuǎn)義字符為'a'

3.使用escape關(guān)鍵字模糊查詢含有'&'的字符串
因?yàn)榇藭r(shí)還是set define off的 所以這個(gè)時(shí)候&并不是什么特殊字符,所以下面的查詢會(huì)報(bào)錯(cuò)
SQL> select * from t_test_escape where name like '%a&%' escape 'a';
 
select * from t_test_escape where name like '%a&%' escape 'a'
 
ORA-01424: missing or illegal character following the escape character
 
SQL> insert into t_test_escape(name) values('Oracle%&hello');
 
1 row inserted
 
SQL> commit;
 
Commit complete
 
SQL> select * from t_test_escape where name like '%a&h%' escape 'a';
 
select * from t_test_escape where name like '%a&h%' escape 'a'
 
ORA-01424: missing or illegal character following the escape character


在set define off關(guān)閉替代變量功能之后可以直接將&當(dāng)做普通字符,而不用escape
SQL> select * from t_test_escape where name like '%&h%';
 
NAME
--------------------
Oracle%&hello
 
使用set define on打開(kāi)替代變量功能
SQL> set define on;
SQL> select * from t_test_escape where name like '%&h%';

--這里會(huì)提示我輸入變量h的值,因?yàn)槲覜](méi)有輸入任何值,這條sql條件相當(dāng)于就是like '%%',所以返回全部數(shù)據(jù)
 
NAME
--------------------
&_hello
%%_hello
Oracle%&_hello
Oracle%&hello
 
--使用escape關(guān)鍵字指定特定的轉(zhuǎn)義字符試試看
SQL> select * from t_test_escape where name like '%a&h%' escape 'a';

 
NAME
--------------------

還是會(huì)提示我輸入變量h的值

--下面通過(guò)查詢出'&'的ascii來(lái)繞過(guò)這個(gè)障礙
SQL> select ascii('&') from dual;
 
ASCII('&')
----------
        38

--使用chr(38)去替代特殊字符'&'
SQL> select * from t_test_escape where name like '%' || chr(38) || 'h%';
 
NAME
--------------------
Oracle%&hello
 

4.使用escape關(guān)鍵字模糊查詢含有'&'的字符串
SQL> select * from t_test_escape where name like '%a_%' escape 'a';
 
NAME
--------------------
&_hello
%%_hello
Oracle%&_hello

5.下面我將替代變量的特殊字符改為$試試,然后看能不能使用模糊匹配匹配特殊字符'&'
SQL> set define $
SQL> select * from t_test_escape where name like '%&h%';
 
NAME
--------------------
Oracle%&hello


總結(jié):對(duì)于使用escape關(guān)鍵字去轉(zhuǎn)義特殊字符的時(shí)候,并不是對(duì)于所有的特殊字符都能夠轉(zhuǎn)義成功,上面的實(shí)驗(yàn)表明,對(duì)于'%', '_', '&'來(lái)說(shuō),使用escape是能夠成功轉(zhuǎn)義'%', '_'的,但是卻不能轉(zhuǎn)義'&',當(dāng)然這只是默認(rèn)的情況,如果我們將綁定變量的標(biāo)識(shí)符設(shè)置為非默認(rèn)的$,那么我們就可以把'&'當(dāng)做普通字符對(duì)待了。如果不改變綁定變量的默認(rèn)標(biāo)識(shí)符,那么就使用chr(38)去替代'&'!





    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多