转载

SQL语句中特殊字符“&”如何转义

一、问题描述
     在SQL语句中“&”符是特殊字符,代表环境变量的输入,比如(select '123&a' from dual; )此处的&a就好比请给变量a赋值,如果我们就是想输出字符串&a这个字符呢,下面的实验中将会给出答案。其实大家会说用转义符就好了,但是这个字符有些特殊,用不了转义,不信可以自己试试。

二、实验
1.创建测试表及数据,以下字符都可以直接当字符串插入,这里的规则"abc特殊字符串def"

  1. SAM@OCM11G >create table test(t varchar2(20));
  2. Table created.

  3. SAM@OCM11G >insert into test values ('abc!def');
  4. 1 row created.

  5. SAM@OCM11G >insert into test values ('abc@def');
  6. 1 row created.

  7. SAM@OCM11G >insert into test values ('abc#def');
  8. 1 row created.

  9. SAM@OCM11G >insert into test values ('abc$def');
  10. 1 row created.

  11. SAM@OCM11G >insert into test values ('abc%def');
  12. 1 row created.

  13. SAM@OCM11G >insert into test values ('abc^def');
  14. 1 row created.

  15. SAM@OCM11G >insert into test values ('abc*def');
  16. 1 row created.

  17. SAM@OCM11G >insert into test values ('abc(def');
  18. 1 row created.

  19. SAM@OCM11G >insert into test values ('abc)def');
  20. 1 row created.

  21. SAM@OCM11G >insert into test values ('abc_def');
  22. 1 row created.

  23. SAM@OCM11G >insert into test values ('abc+def');
  24. 1 row created.

  25. SAM@OCM11G >insert into test values ('abc=def');
  26. 1 row created.

  27. SAM@OCM11G >insert into test values ('abc-def');
  28. 1 row created.

  29. SAM@OCM11G >insert into test values ('abc`def');
  30. 1 row created.

  31. SAM@OCM11G >insert into test values ('abc~def');
  32. 1 row created.

  33. SAM@OCM11G >insert into test values ('abc|def');
  34. 1 row created.

  35. SAM@OCM11G >insert into test values ('abc;def');
  36. 1 row created.

  37. SAM@OCM11G >insert into test values ('abc?/def');
  38. 1 row created.

  39. SAM@OCM11G >insert into test values ('abc<>def');
  40. 1 row created.

  41. SAM@OCM11G >commit;
  42. Commit complete.
2.让我来测试一下’&’字符,直接使用&字符,无法得到想要的结果“abc&def”,在此处’&’字符做为输入变量参数的关键字来用

  1. SAM@OCM11G >insert into test values ('abc&def'); --此处“&”当作输入符来对待
  2. Enter value for def: &def --为了实现源字符,输入“&def”
  3. old 1: insert into test values ('abc&def')
  4. new 1: insert into test values ('abc&def')

  5. 1 row created.

  6. SAM@OCM11G >select * from test;

  7. T
  8. --------------------
  9. abc!def
  10. abc@def
  11. abc#def
  12. abc$def
  13. abc%def
  14. abc^def
  15. abc*def
  16. abc(def
  17. abc)def
  18. abc_def
  19. abc+def
  20. abc=def
  21. abc-def
  22. abc`def
  23. abc~def
  24. abc|def
  25. abc;def
  26. abc?/def
  27. abc<>def
  28. abc&def

  29. 20 rows selected.
3.使用正常转义符,仍然报错:缺失或非法字符转义字符
(1)首先让我们使用转义符来查询一个带有’%’符号的数据

  1. SCOTT@OCM11G >select * from test where t like '%/%%' escape '/';

  2. T
  3. --------------------
  4. abc%def
(2)再让我们用该方法查询带有’&’符号的数据,结果失败

  1. SCOTT@OCM11G >select * from test where t like '%/&%' escape '/';
  2. select * from test where t like '%/&%' escape '/'
  3.                                 *
  4. ERROR at line 1:
  5. ORA-01424: missing or illegal character following the escape character
  6. ORA-01424: 缺失或非法字符转义字符
4.通过&字符的ascii值来解决该问题
(1)查询出’&’字符的ascii值

  1. SYS@OCM11G >select ascii('&') from dual;

  2. ASCII('&')
  3. ----------
  4.         38
(2)将ascii值代入字符串中(注意使用连接符)

  1. SCOTT@OCM11G >select 'abc'||chr(38)||'def' from dual;

  2. 'ABC'||
  3. -------
  4. abc&def

  5. SCOTT@OCM11G >insert into test values ('abc'||chr(38)||'def');

  6. 1 row created.

  7. SCOTT@OCM11G >commit;

  8. Commit complete.
(3)验证最后插入数据

  1. SCOTT@OCM11G >select * from test;

  2. T
  3. --------------------
  4. abc!def
  5. abc@def
  6. ...
  7. abc<>def
  8. abc&def --使用&&def插入
  9. abc&def --使用ascii编码插入

  10. 21 rows selected.
三、总结
     如果想使用特殊’&’字符当字符串来用时,可以使用ascii值来代替,这样可以解决SQL语句中的特殊需求。当然,这种变态的要求很难碰到。ORACLE小技巧,有时需要变通的思想,当然我也是借鉴了别的思路。学习不止,Sam继续加油! Where there is a will, there is a way.








正文到此结束
Loading...