2012-10-18 36 views
79

我想在我的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); 

雖然這與this question具有類似的解決方案,但問題卻有很大不同。他們最終可能會得到同樣的解決方案,但這並不意味着問題是相同的。

+0

我不認爲你需要圍繞escape('\')字符引用 – mcalex

+17

'set define off'是最簡單的方法。 – Annjawn

+1

Duplicate http://stackoverflow.com/questions/118190/how-do-i-ignore-ampersands-in-a-sql-script-running-from-sql-plus – LordScree

回答

124

而不是

node_name = 'Geometric Vectors \& Matrices' 

使用

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

38是符號的ASCII碼,並以這種形式將被解釋爲一個字符串,沒有別的。我試過了,它工作。

另一種方式,可以使用LIKE和下劃線,而不是「&」字:

node_name LIKE 'Geometric Vectors _ Matrices' 

,你會找到一些其他記錄過的機會,這是隻有這一個字不同,是相當低。從的Oracle SQL基本面書

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

+28

代替不直觀的'chr(38)',你可以做'node_name ='Geometric Vectors&'|| '矩陣' –

27

您可以使用

set define off 

使用該不會提示輸入

81

Escape is set to \ by default,所以你不需要設置;但是如果你這樣做,不要用引號將它包裹起來。

&符號是SQL * Plus substitution variable標記;但你可以change it,或者更有效地在你的情況下將其關閉完全,有:

set define off 

那麼你也沒必要理會所有逃逸的價值。

+0

完全同意! – Annjawn

+1

默認情況下,escape *字符*被設置爲'\',但布爾參數'escape'默認設置爲OFF。所以OP可能不需要設置轉義字符,但他/她至少需要'SET ESCAPE ON'才能工作。或者,當然,使用其他更好的解決方案。 – mathguy

19

直一個會怎麼逃生呢竟未定義。

+2

SET DEFINE ON的添加很有幫助。 – KSev

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

這工作,以及:

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

您可以通過內附定義&爲文字與字符串中的雙qoutes "&"

+0

如果你這樣做,雙引號將成爲字符串的一部分,如下所示:'A3D「&」R01' –

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

或者:

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

第一個允許您使用反斜槓逃脫&。

第二個關閉&「全局」(不需要在任何地方添加反斜槓)。您可以通過set define off再次打開它,並且從&符號上的該行將返回其特殊含義,因此您可以在腳本的某些部分關閉它,而不是爲其他人關閉它。