2013-02-04 68 views
2

我需要在dbms_output語句中包含單引號。我已經試過這樣:
dbms_output語句中的單引號?

dbms_output.put_line('\''first_name||'\''); 

這裏FIRST_NAME正在variable;我需要在單引號內顯示​​。

+0

如果first_name擁有xyx,那麼我的輸出將是'xyz'。這是我試圖獲得的wat。 –

+0

可能重複的[如何在Oracle SQL中處理單引號](http://stackoverflow.com/questions/2875257/how-to-handle-a-single-quote-in-oracle-sql) –

+0

可能的重複[如何在Oracle SQL中處理單引號](https://stackoverflow.com/questions/2875257/how-to-handle-a-single-quote-in-oracle-sql) –

回答

7

你會加倍了逃避:

dbms_output.put_line('''' || first_name || ''''); 

或使用q-quote機制:

dbms_output.put_line(q'[']'||first_name||q'[']'); 

如:

SQL> var b1 varchar2(30) 
SQL> exec :b1 := 'this is a test'; 

PL/SQL procedure successfully completed. 

SQL> exec dbms_output.put_line(q'[']'||:b1||q'[']'); 
'this is a test' 

PL/SQL procedure successfully completed. 

SQL> exec dbms_output.put_line(''''||:b1||''''); 
'this is a test' 

PL/SQL procedure successfully completed. 

SQL> exec dbms_output.put_line(chr(39)||:b1||chr(39)); 
'this is a test' 

PL/SQL procedure successfully completed. 
+0

擊敗我2秒:) – Randy

+0

亞傢伙我已經得到了使用第一個輸出,但我似乎也非常乏味非結構化!劑量第二個作品!? –

+0

@Thiyagu是的,它的工作原理。 – DazzaL

0

請注意,這只是針對PL/SQL並且必須是積極的。

begin 
DBMS_OUTPUT.PUT_LINE('DELETE FROM MYTABLE WHERE MYFIELD <> ''A'';'); 
end; 
/

假設您想匹配CHAR'A'。

相關問題