ORA-00911错误 解决实例

2015-07-16 12:09:21 · 作者: · 浏览: 2

话说回来,这个ORA-00911的错误,是在一段用JAVA写的测试用例中碰到的,


...


private static final String SQL_INSERT_TBL = "insert into tbl (id, ...) "
? ? ? ? ? ? + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE, SYSDATE);";


报的是这段SQL有这个错误。


看看错误描述:


ORA-00911 invalid character
Cause: Special characters are valid only in certain places. If special characters other than $, _, and # are used in a name and the name is not enclosed in double quotation marks ("), this message will be issued. One exception to this rule is for database names; in this case, double quotes are stripped out and ignored.
Action: Remove the invalid character from the statement or enclose the object name in double quotation marks.


很明显,这个错误主要是因为SQL中包含了非法字符,解析的时候出现报错。


但这个SQL很简单啊,就是一条INSERT语句,将他拷贝到plsql developer中好像是可以的啊,奇怪了。


调试很多次后,忽然发现,定义中结尾有个“;”,难道是这个的问题?


于是首先在plsql developer中执行INSERT语句,结尾带了两个“;”,果然报了这个ORA-00911错误。


修改代码中的语句:


private static final String SQL_INSERT_TBL = "insert into tbl (id, ...) "
? ? ? ? ? ? + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, SYSDATE, SYSDATE)";
?


果然,正常回显了。


一个看似简单,但挑错较麻烦的错误,值得自己的总结。


1. OERR错误描述是基准,要顺着这个方向排查,基本方向可以明确。


2. 代码中出现的SQL错误,可以在plsql developer等工具中重新执行来模拟错误,但一定要和代码中的SQL语句一样,这里我可能拷贝的时候就没有带结尾的;,以至于第一次没有发现这个错误,总之一句话,除了需要判断大方向,最重要的一点就是细心,不要放过任何一个小的细节。