quoting string literals in 10g
http://www.oracle-developer.net/display.php?id=311
This short article introduces Oracle's new quoting mechanism in PL/SQL. This is a new feature of 10g that enables us to embed single-quotes in literal strings without having to resort to double, triple or sometimes quadruple quote characters. This is particularly useful for building dynamic SQL statements that contain quoted literals.
The mechanism is invoked with a simple "q" in PL/SQL only. The syntax is q'[...]', where the "[" and "]" characters can be any of the following as long as they do not already appear in the string.
- !
- [ ]
- { }
- ( )
- < >
Note that at the time of writing, the quoting mechanism only appears to work with 10g clients/OCI. If used with any software that uses an older Oracle client it fails with ORA-01756: quoted string not properly terminated (confirmed with sqlplus, TOAD and PL/SQL Developer).
a simple example
The following example demonstrates how simple it is to use this mechanism when building a string that contains single quotes.
SQL> DECLARE 2 v VARCHAR2(1024); 3 BEGIN 4 v := q'[It's a string with embedded quotes...]'; 5 DBMS_OUTPUT.PUT_LINE(v); 6 END; 7 / It's a string with embedded quotes... PL/SQL procedure successfully completed.
using the quoting mechanism in dynamic sql
Many developers will be familiar with dynamic SQL. While providing a flexible solution to many programming problems, dynamic SQL can also be difficult to build, debug and support. The quoting mechanism doesn't make these problems go away, but it at least makes dynamic SQL a little bit easier to transfer between SQL editors and packages during development.
The following is a contrived example of a dynamic SQL statement that includes literal quotes in the string. The sharp-witted amongst us will notice that this statement doesn't in fact need to be dynamic (and even if it did, it should be using bind variables). However, in the interest of demonstrating the quoting mechanism in as simple and short an example as possible, we'll stick with it.
SQL> DECLARE 2 v_sql VARCHAR2(1024); 3 v_cnt PLS_INTEGER; 4 BEGIN 5 v_sql := q'[SELECT COUNT(*) FROM user_objects WHERE object_type = 'TABLE']'; 6 EXECUTE IMMEDIATE v_sql INTO v_cnt; 7 DBMS_OUTPUT.PUT_LINE( 8 TO_CHAR(v_cnt) || ' tables in USER_OBJECTS.' 9 ); 10 END; 11 / 4 tables in USER_OBJECTS. PL/SQL procedure successfully completed.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15480802/viewspace-687863/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- C++ Gotchas 條款64:丟擲String Literals (轉)C++Go
- Bourne shell script中的quoting
- 1211Bug with integer literals in PLSQLSQL
- JavaScript 有趣的冷知識:tagged template literalsJavaScript
- String,String Builder,String Buffer-原始碼UI原始碼
- Failed to execute user defined function(anonfun$concatStr$1: (map<string,string>, string) => string)AIFunction
- 文獻閱讀——Single Clause Assumption without Activation Literals to Speed-up IC3
- String a = "abc" 與String b = new String("abc")的區別
- java.lang.String 與stringJava
- String
- String s = “hello“和String s = new String(“hello“)的區別
- 【JDK】分析 String str=““ 與 new String()JDK
- rust 中 str 與 String; &str &StringRust
- [JAVA]toString()、String.valueOf()、(String)Java
- ${string::N}和${string:N}字元提取字元
- 轉換String三種方式比較:toString()、String.valueOf()、(String)
- C#中String和string區別C#
- String字串字串
- JavaScript String()JavaScript
- string 字串字串
- ES 筆記十:Query String & Simple Query String筆記
- String str=null; 和String str=""的區別Null
- String s = new String(" a ") 到底產生幾個物件?物件
- PHP Fatal error: Cannot use PhpParser\Node\Scalar\String as StringPHPError
- String s=new String("abc")建立了幾個物件?物件
- Convert string to binary and binary to string in C#C#
- python stringPython
- Elasticsearch——query stringElasticsearch
- String筆記筆記
- python stringPython
- JavaScript String 字串JavaScript字串
- javascript-StringJavaScript
- Java String類Java
- Swift Int to StringSwift
- C# stringC#
- ORACLE escape stringOracle
- spring - stringSpring
- D - String Bags