問(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)去替代'&'! |
|
來(lái)自: liuyang_inf > 《技術(shù)文檔》