【问题标题】:Escaping ampersand character in SQL string在 SQL 字符串中转义 & 字符
【发布时间】:2012-10-18 18:23:10
【问题描述】:

我试图在我的 sql 数据库中按名称查询某个行,它有一个 & 符号。我试图设置一个转义字符,然后转义 & 符号,但由于某种原因,这不起作用,我不确定我的问题到底是什么。

Set escape '\'
    select * from V1144engine.T_nodes where node_id in(
    select node2_id from V1144engine.T_edges where node1_id in(
    select node2_id from V1144engine.T_edges where node1_id in(
    select node2_id from V1144engine.T_edges where node1_id = 
      (select node_id from V1144engine.T_nodes where node_name = 'Geometric Vectors \& Matrices')))
    and edge_type_id = 1)
    and node_type_id = 1
    and node_id in (
    select node2_id from V1144engine.T_edges where node1_id =
      (select node_id from V1144engine.T_nodes where node_name = 'Algebra II')
    and edge_type_id = 2);

虽然这与this question 有类似的解决方案,但问题的提出却截然不同。他们可能最终得到相同的解决方案,但这并不意味着问题是相同的。

【问题讨论】:

标签: sql oracle escaping sqlplus


【解决方案1】:

代替

node_name = 'Geometric Vectors \& Matrices'

使用

node_name = 'Geometric Vectors ' || chr(38) || ' Matrices' 

38 是 & 符号的 ascii 代码,在这种形式下,它将被解释为字符串,仅此而已。我试过了,它奏效了。

另一种方法是使用 LIKE 和下划线代替 '&' 字符:

node_name LIKE 'Geometric Vectors _ Matrices' 

你也能找到其他记录的机会,仅在这一个角色上有所不同,这是非常低的。

【讨论】:

  • 不用不直观的chr(38),你可以做node_name = 'Geometric Vectors &' || ' Matrices'
  • 对于和我一样的其他可怜的困惑的灵魂,请注意它是chr(38)不是 char(38)
  • 请注意,& 在 MySQL 中不需要转义。 MySQL也没有CHR()的功能。
【解决方案2】:

Escape is set to \ by default,所以不需要设置;但如果这样做,请不要用引号括起来。

& 是 SQL*Plus substitution variable 标记;但你可以change it,或者更有用的是在你的情况下完全关闭它,使用:

set define off

那么你根本不需要费心转义值。

【讨论】:

  • 转义字符默认设置为\ ,但布尔参数escape默认设置为OFF。所以 OP 可能不需要设置转义字符,但他/她至少需要 SET ESCAPE ON 才能工作。或者,当然,使用任何其他更好的解决方案。
【解决方案3】:

你可以使用

set define off

使用它不会提示输入

【讨论】:

    【解决方案4】:

    直接来自 oracle sql 基础书籍

    SET DEFINE OFF
    select 'Coda & Sid' from dual;
    SET DEFINE ON
    

    如果不设置define,怎么能逃脱它。

    【讨论】:

    • 添加 SET DEFINE ON 很有帮助。
    • @Roman 没有定义你也可以使用:select 'Coda''&'' Sid' from dual; (双单引号)
    【解决方案5】:

    为了逃避&你可以尝试以下方法:-

    1. set scan off
    2. set define off
    3. set escape on 然后将& 替换为\&
    4. & 替换为&&

    其中一个应该至少可以工作。

    另外,如果您使用的是 PL/SQL 开发人员,那么 SQL 窗口底部会有 & icon 请前往那里并禁用 .请注意,在旧版本中,此选项不存在。

    还要确保 set define off 写在脚本的开头。

    【讨论】:

    • 在第 3 步中,不应该是\& 吗?
    • 更新答案@Manngo
    【解决方案6】:
    set escape on
    ... node_name = 'Geometric Vectors \& Matrices' ...
    

    或者:

    set define off
    ... node_name = 'Geometric Vectors & Matrices' ...
    

    第一个允许您使用反斜杠来转义 &。

    第二个关闭并“全局”(无需在任何地方添加反斜杠)。您可以通过set define on 再次将其打开,并且从该行中的符号将恢复其特殊含义,因此您可以在脚本的某些部分而不是其他部分将其关闭。

    【讨论】:

      【解决方案7】:

      这也有效:

      select * from mde_product where cfn = 'A3D"&"R01'

      您将& 定义为文字,方法是在字符串中包含双引号"&"

      【讨论】:

      • 如果这样做,双引号将成为字符串的一部分,如下所示:A3D"&"R01
      【解决方案8】:
      REPLACE(<your xml column>,'&',chr(38)||'amp;')
      

      【讨论】:

        【解决方案9】:

        我写了一个正则表达式来帮助在 INSERT 中查找和替换“&”,我希望这对某人有所帮助。

        诀窍是确保“&”与其他文本一起使用。

        查找“(\'[^\']*(?=\&amp;))(\&amp;)([^\']*\')”

        替换“$1' || chr(38) || '$3”

        【讨论】:

          【解决方案10】:
          --SUBSTITUTION VARIABLES
          -- these variables are used to store values TEMPorarily.
          -- The values can be stored temporarily through
          -- Single Ampersand (&)
          -- Double Ampersand(&&)
          -- The single ampersand substitution variable applies for each instance when the
          --SQL statement is created or executed.
          -- The double ampersand substitution variable is applied for all instances until
          --that SQL statement is existing.
          INSERT INTO Student (Stud_id, First_Name, Last_Name, Dob, Fees, Gender)
          VALUES (&stud_Id, '&First_Name' ,'&Last_Name', '&Dob', &fees, '&Gender');
          --Using double ampersand substitution variable
          INSERT INTO Student (Stud_id,First_Name, Last_Name,Dob,Fees,Gender)
          VALUES (&stud_Id, '&First_Name' ,'&Last_Name', '&Dob', &&fees,'&gender');
          

          【讨论】:

          • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
          【解决方案11】:

          我知道这很糟糕。以上所有事情都没有真正起作用,但我找到了解决方法。请特别注意在撇号 [ ' ] 之间使用空格,否则会被转义。

          SELECT 'Hello ' '&' ' World';
          
          Hello & World
          

          不客气;)

          【讨论】:

            猜你喜欢
            • 2016-03-07
            • 2012-05-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-19
            相关资源
            最近更新 更多