13. View the Exhibit and examine the structure of the PRODUCTS table.
You need to generate a report in the following format:
CATEGORIES
5MP Digital Photo Camera's category is Photo
Y Box's category is Electronics
Envoy Ambassador's category is Hardware
Which two queries would give the required output? (Choose two.)
A. SELECT prod_name q'''s category is ' prod_category CATEGORIES
FROM products;
B. SELECT prod_name q'['s ]'category is ' prod_category CATEGORIES
FROM products;
C. SELECT prod_name q'\'s\' ' category is ' prod_category CATEGORIES
FROM products;
D. SELECT prod_name q'<'s >' 'category is ' prod_category CATEGORIES
FROM products;
注意:选项中字符串之间的连接应加连接符||
Answer: CD
题目解析:
sql中字符串的两边是须要加单引號的,表示字符串的開始和结束,假设我们想在sql中输出一个实际的单引號',那么须要连续输入四个单引號'''',当中中间的两个单引號表示一个单引號。
第一个单引號:表示字符串開始
第二个单引號:表示转义,后面接的单引號转义为真正的单引號
第三个单引號:表示实际输出的单引號
第四个单引號:表示字符串结束
如:
SQL> select '''' name from dual;
N
-
'
SQL> select 'Camera''s category is Photo' name from dual;
NAME
--------------------------
Camera's category is Photo
另外Oracle还提供了一个Q-quote的表达式,来简化SQL或PLSQL中字符串的表示,其格式为q'[Camera's category is Photo]',输出为方括号里的原始字符串格式,当中方括号能够更换为其它的随意一对特殊符号,但必须是成对出现的,不可为q'[Camera's category is Photo|'。
如:
SQL> select q'[Camera's category is Photo]' name from dual;
NAME
--------------------------
Camera's category is Photo
SQL> select q'|Camera's category is 'Photo'|' name from dual;
NAME
----------------------------
Camera's category is 'Photo'
A:错误, is后面应该是两个单引號,正确的为
SELECT prod_name || q'''s category is ''|| prod_category CATEGORIES
FROM products;
B:错误,category 前面少了一个',正确的为
SELECT prod_name || q'['s ]'||'category is '||prod_category CATEGORIES FROM products;