Oracle查询给出错误-SQL命令未正确结束

时间:2019-05-03 03:44:44

标签: java sql database oracle jdbc

我在Oracle DB上的查询下面手动运行,并且运行良好并给出了结果。问号将被替换为正确的值。

DROP TABLE EventTable PURGE  CREATE GLOBAL TEMPORARY TABLE EventTable ( Module NVARCHAR2(512) NULL,EventType NVARCHAR2(512) NULL,ModuleAndEventText NVARCHAR2(512) NULL,Source NVARCHAR2(512) NULL,Severity NVARCHAR2(512) NULL,Node NVARCHAR2(512) NULL,UserSID NVARCHAR2(512) NULL,DesktopId NVARCHAR2(512) NULL,MachineId NVARCHAR2(512) NULL,FolderPath NVARCHAR2(512) NULL,LUNId NVARCHAR2(512) NULL,ThinAppId NVARCHAR2(512) NULL,EndpointId NVARCHAR2(512) NULL,UserDiskPathId NVARCHAR2(512) NULL,GroupId NVARCHAR2(512) NULL,EventID NUMBER NOT NULL,Time TIMESTAMP(6) NULL,Acknowledged NUMBER(38,0)NULL, PRIMARY KEY (EventID))  INSERT INTO EventTable ( Module,EventType,ModuleAndEventText,Source,Severity,Node,UserSID,DesktopId,MachineId,FolderPath,LUNId,ThinAppId,EndpointId,UserDiskPathId,GroupId,EventID,Time,Acknowledged ) SELECT Module,EventType,ModuleAndEventText,Source,Severity,Node,UserSID,DesktopId,MachineId,FolderPath,LUNId,ThinAppId,EndpointId,UserDiskPathId,GroupId,EventID,Time,Acknowledged FROM sdevent WHERE (UPPER(UserSID) = ?) AND (Time BETWEEN ? AND ?) AND ((UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?)) SELECT COUNT(*)  FROM EventTable E  LEFT OUTER JOIN ((SELECT* FROM ( SELECT sdevent_data.eventid, name, strValue FROM sdevent_data INNER JOIN EventTable ET ON sdevent_data.eventid=ET.eventid) SourceTable PIVOT (MAX(strValue) FOR name IN ('ApplicationId','MachineId','RDSServerId','FarmId','SessionId','UserDisplayName','DesktopDisplayName','ApplicationDisplayName','MachineName','FarmDisplayName','EndUserDisplayName','ThinAppDisplayName','ProcessName','RemoteApplicationId','RemoteApplicationDescription')) PivotTable)) strValueTable ON E.eventid=strValueTable.eventid LEFT OUTER JOIN (SELECT EventID,intValue FROM sdevent_data WHERE (Name = 'ProcessId')) ProcessId ON E.EventID = ProcessId.EventID LEFT OUTER JOIN (SELECT EventID,strBlobValue FROM sdevent_data WHERE (Name = 'TimingProfilerTree')) TimingProfilerTree ON E.EventID = TimingProfilerTree.EventID WHERE (UPPER(UserSID) = ?) AND (Time BETWEEN ? AND ?) AND ((UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?) OR (UPPER(EventType) = ?))

但是当我从Java JDBC运行相同的查询时,它给了我一个SQL异常-

 java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:225)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:53)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:943)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1150)

下面是创建上述查询的Java代码的片段-

StringBuilder countQuery = new StringBuilder();

       countQuery.append(" DROP TABLE EventTable PURGE ");
       countQuery.append(" CREATE GLOBAL TEMPORARY TABLE EventTable ( ");
       countQuery.append(getTempTableAttributes());
       countQuery.append(" INSERT INTO EventTable ( ");
       countQuery.append(getEventTableAttributes());
       countQuery.append(" ) SELECT ");
       countQuery.append(getEventTableAttributes());
       countQuery.append(" FROM ");
       countQuery.append(this.eventTableName);
       if (def.filter != null) {
           countQuery.append(" WHERE ");
           countQuery.append(applyFilterToQuery(def.filter));
       }
       countQuery.append(" SELECT COUNT(*) ");
       countQuery.append(" FROM ");
       countQuery.append("EventTable E ");
       countQuery.append(getLeftJoinClause());
       if (def.filter != null) {
           countQuery.append(" WHERE ");
           countQuery.append(applyFilterToQuery(def.filter));
       }
       String query = countQuery.toString();

当我打印字符串查询时,它会打印上面的查询,效果很好。因此,不确定从Java运行时出了什么问题。有什么方法可以查看在Oracle DB中运行的类似于MS SQL Profiler的实际查询吗?

1 个答案:

答案 0 :(得分:1)

如果查询运行正常,我怀疑查询参数可能存在问题。执行查询时设置调试点,并检查替换的参数。