oracle - 在SQL字符串中转义&符号
我试图在我的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);
虽然这与这个问题有类似的解决方案,但问题却有很大不同。 他们可能最终得到相同的解决方案,但这并不意味着问题是相同的。
8个解决方案
171 votes
代替
node_name = 'Geometric Vectors \& Matrices'
使用
node_name = 'Geometric Vectors ' || chr(38) || ' Matrices'
38是&符号的ascii代码,在这种形式中它将被解释为一个字符串,没有别的。 我尝试了它,它的工作原理。
另一种方法可能是使用LIKE和下划线而不是'&' 字符:
node_name LIKE 'Geometric Vectors _ Matrices'
您也可以找到其他记录的机会非常低,这个记录在这一个字符中是不同的。
Imre Greilich answered 2019-07-05T01:27:34Z
93 votes
Escape默认设置为\,因此您无需进行设置; 但如果你这样做,不要用引号括起来。
Ampersand是SQL * Plus替换变量标记; 但你可以改变它,或者更有用的是在你的情况下完全关闭它,用:
set define off
那么你根本不需要费心去逃避价值。
Alex Poole answered 2019-07-05T01:28:12Z
36 votes
您可以使用
set define off
使用它,它不会提示输入
Ankur answered 2019-07-05T01:28:39Z
25 votes
直接来自oracle sql基础知识书
SET DEFINE OFF
select 'Coda & Sid' from dual;
SET DEFINE ON
没有设置定义,如何逃脱它。
Roman answered 2019-07-05T01:29:10Z
2 votes
为了逃避&,您可以尝试以下方式: -
&
&
&然后替换&& by/&
替换&由&&
其中一个应至少工作。
此外,如果您正在使用PL / SQL开发人员,那么还有& sql窗口底部的图标请去那里并禁用它。 请注意,在旧版本中,此选项不存在。
还要确保在脚本的最开头写入set define off。
Shivendra Gupta answered 2019-07-05T01:30:20Z
1 votes
set escape on
... node_name = 'Geometric Vectors \& Matrices' ...
或者:
set define off
... node_name = 'Geometric Vectors & Matrices' ...
第一个允许你使用反斜杠来逃避&amp ;.
第二个关闭&"全局" (无需在任何地方添加反斜杠)。 您可以通过set define on重新打开它,并且&符号上的那一行将获得它们的特殊含义,因此您可以将其关闭以用于脚本的某些部分而不是其他部分。
David Balažic answered 2019-07-05T01:30:56Z
0 votes
REPLACE(,'&',chr(38)||'amp;')
user6385350 answered 2019-07-05T01:31:15Z
0 votes
这也有效:
&
您将&定义为字面值,并在字符串中使用双引号"&"。
copmac answered 2019-07-05T01:31:50Z