在默认情况下,ORACLE允许用户在表字段里使用&字符和其他一些特殊字符。
SQL> select * from v$version;
BANNER
----------------------------------------------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit
PL/SQL Release 10.2.0.3.0 - Production
CORE10.2.0.3.0Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 – Production
SQL> create table t (name varchar(20));
Table created.
SQL> insert into t values('&');
1 row created.
SQL> insert into t values('&!@#$%^*()_+');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t;
NAME
--------------------
&
&!@#$%^*()_+
不过由于&可以被解释为替换字符(Substitution Character),所以有一定的特殊性。
记得前几天一同事在生产数据库上期望在字段中插入&字符,情况类似如下:
SQL> insert into t values('&1');
结果ORACLE将&1解释为了替换变量(Substitution Variables),要求输入1变量的value。
Enter value for 1:
同事问我怎么办?我第一个反应就是:再输入个&1。
SQL> insert into t values('&1');
Enter value for 1: &1
old1: insert into t values('&1')
new1: insert into t values('&1')
1 row created.
SQL>commit;
Commit complete.
SQL> select * from t;
NAME
--------------------
&
&!@#$%^*()_+
&1
果然,&1被正确插入到表中了。
除了我上述说的比较讨巧的方法,SQLPLUS下处理&字符的方法还有如下几个:
(1)关闭对于替换字符的定义
SQL>set define off
SQL> insert into t values('&2');
1 row created.
SQL>commit;
Commit complete.
(2)将替换字符的定义设置为其他不用的字符
SQL> set define %
SQL>insert into t values('&3');
1 row created.
SQL>commit;
Commit complete.
(3)设定字符转义
SQL>set escape on
SQL>show escape
escape "\" (hex 5c)
SQL>insert into t values('\&4');
1 row created.
SQL> commit;
Commit complete.
SQL>select * from t;
NAME
--------------------
&
&!@#$%^*()_+
&1
&2
&3
&4
6 rows selected.