转义SQL字符串中的&符号

时间:2012-10-18 18:23:10

标签: sql oracle escaping sqlplus

我正在尝试在我的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有类似的解决方案,但问题却截然不同。他们最终可能会有相同的解决方案,但这并不意味着问题是相同的。

11 个答案:

答案 0 :(得分:171)

而不是

node_name = 'Geometric Vectors \& Matrices'

使用

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

38是&符号的ascii代码,在这种形式下,它将被解释为字符串,没有别的。我尝试了它,它的工作原理。

另一种方法可能是使用LIKE和下划线而不是'&'性格:

node_name LIKE 'Geometric Vectors _ Matrices' 

你也会找到其他记录的可能性非常低,只有这一个字符有所不同。

答案 1 :(得分:95)

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

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

set define off

然后你根本不需要去逃避价值。

答案 2 :(得分:38)

您可以使用

set define off

使用它不会提示输入

答案 3 :(得分:26)

直接来自oracle sql基础知识书

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

如果不设置define,如何逃脱它。

答案 4 :(得分:2)

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

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

其中一个应该至少工作。

A 另外,如果您使用的是PL / SQL开发人员,则sql窗口底部有&图标,请转到该位置并禁用 。请注意,在较早的版本中,此选项不存在。

还要确保将set define off写在脚本的最开始。

答案 5 :(得分:1)

set escape on
... node_name = 'Geometric Vectors \& Matrices' ...

或者:

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

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

第二次关闭& "全局" (无需在任何地方添加反斜杠)。您可以通过set define on再次打开它,并且&符号上的那一行将获得它们的特殊含义,因此您可以将其关闭以用于脚本的某些部分而不是其他部分。

答案 6 :(得分:0)

REPLACE(<your xml column>,'&',chr(38)||'amp;')

答案 7 :(得分:0)

这也有效:

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

您将&定义为文字,方法是在字符串中使用双qoutes "&"括起来。

答案 8 :(得分:0)

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

诀窍是确保“&”与其他文字保持一致。

找到“(\'[^\']*(?=\&))(\&)([^\']*\')”

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

答案 9 :(得分:0)

--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');

答案 10 :(得分:-1)

我知道那很烂。以上所有内容均未真正起作用,但我找到了解决方法。请格外小心,在撇号[']之间使用空格,否则它将被转义。

typedef

不客气;)

相关问题