oracle 我在使用mybatis执行sql时遇到了ORA-00907异常

guykilcj  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(169)

我试图从我的数据库查询一些数据,这是我的SQL代码,它在控制台运行顺利:

select  BTN_NAME as btnName,                
    LAST_EXEC_TIME as lastExecTime,                
    LAST_EXEC_PARAM as lastExecParam         
from BTN_FUNC_FLOW_CTRL
where BTN_NAME = 'REG_FILE_COLLECT' AND LAST_EXEC_TIME >= (SYSTIMESTAMP - INTERVAL '60' MINUTE);

但是当我在mybatis中编写这个sql并执行的时候,出了点问题,我得到了一个ORA-00907

<select id="getByBtnName" resultType="xxx.BtnFuncFlowCtrl">
    select BTN_NAME as btnName,
           LAST_EXEC_TIME as lastExecTime,
           LAST_EXEC_PARAM as lastExecParam
    from regulatory.BTN_FUNC_FLOW_CTRL
    where BTN_NAME = #{btnName} and LAST_EXEC_TIME >= (SYSTIMESTAMP - INTERVAL #{execInterval} MINUTE);
</select>
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: ORA-00907: missing right parenthesis

### The error may exist in file [C:\Users\Administrator\IdeaProjects\xxx\BtnFuncFlowCtrlMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select BTN_NAME as btnName,                LAST_EXEC_TIME as lastExecTime,                LAST_EXEC_PARAM as lastExecParam         from regulatory.BTN_FUNC_FLOW_CTRL         where BTN_NAME = ? and LAST_EXEC_TIME >= (SYSTIMESTAMP - INTERVAL ? MINUTE);
### Cause: java.sql.SQLSyntaxErrorException: ORA-00907:missing right parenthesis

当我去掉括号,我得到了一个ORA-00933

<select id="getByBtnName" resultType="xxx.BtnFuncFlowCtrl">
    select BTN_NAME as btnName,
           LAST_EXEC_TIME as lastExecTime,
           LAST_EXEC_PARAM as lastExecParam
    from regulatory.BTN_FUNC_FLOW_CTRL
    where BTN_NAME = #{btnName} and LAST_EXEC_TIME >= SYSTIMESTAMP - INTERVAL #{execInterval} MINUTE;
</select>
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

### The error may exist in file [C:\Users\Administrator\IdeaProjects\xxx\BtnFuncFlowCtrlMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select BTN_NAME as btnName,                LAST_EXEC_TIME as lastExecTime,                LAST_EXEC_PARAM as lastExecParam         from regulatory.BTN_FUNC_FLOW_CTRL         where BTN_NAME = ? and LAST_EXEC_TIME >= SYSTIMESTAMP - INTERVAL ? MINUTE;
### Cause: java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

我想知道为什么会出现这些问题以及如何解决它们。

zlhcx6iw

zlhcx6iw1#

Interval literal应该有literals作为其组成部分:它应该是恒定的。

文字

术语“文字”和“常数值”是同义词,指的是固定的数据值。
你试图组合的不是一个常量,因为它有bind variable

SQL> variable i varchar2(10);
SQL> 
SQL> exec :i := '10';

PL/SQL procedure successfully completed.

SQL> 
SQL> select *
  2  from dual
  3  where systimestamp > systimestamp - interval :i minute
  4  ;

Error starting at line : 7 in command -
select *
from dual
where systimestamp > systimestamp - interval :i minute

Error at Command Line : 9 Column : 46
Error report -
SQL Error: ORA-00933: неверное завершение SQL-предложения
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:

但是,您可以使用间隔算法,并指定间隔的基值,然后将其乘以所需的单位数:

SQL> select dual.*, interval '1' minute * to_number(:i) as interval_value
  2  from dual
  3  where systimestamp > systimestamp - interval '1' minute * to_number(:i)
  4  ;

D INTERVAL_VALUE     
- -------------------
X +00 00:10:00.000000

1 row selected.

或者使用numtodsinterval函数作为变量单位:

SQL> variable u varchar2(10);
SQL> exec :u := 'minute';

PL/SQL procedure successfully completed.

SQL> 
SQL> select dual.*, numtodsinterval(to_number(:i), :u) as interval_value
  2  from dual
  3  where systimestamp > systimestamp - numtodsinterval(to_number(:i), :u)
  4  ;

D INTERVAL_VALUE     
- -------------------
X +00 00:10:00.000000

1 row selected.

相关问题