ORACLE使用过程中经常遇到一些没遇到的错误,这里做个简单的总结
INSERT INTO t_b_airstationinfo VALUES ('084ecdeda60c8700e9392d9d41c261af', '1007A', '武术学校', null, '湾里区', null, '115.74222200', '28.79944400', '南昌市湾里区梅岭店前街梅岭宾馆内', null, null, '40287e816714ec9201671642b093002c', '3', '2021-03-15 15:06:32.000000', null, '1', '40287e816714ec9201671642b093002c');
在数据库转储的过程中,TimeStamp字段报错。
原因是TimeStamp类型不一致。显然要插入的数据格式的TimeStamp日期格式为YYYY-MM-DD HH24:MI:SS.FF6。修改会话参数使得符合TimeStamp格式即可。
alter session set nls_timestamp_format = ‘YYYY-MM-DD HH24:MI:SS.FF6’;
需要SQL转储,结果运行SQL文件全部报错。
既然SQL文件是导出的,那肯定是没什么问题。查看字符集后发现没有问题,那么再看看是否原数据库与现数据库类型不一致导致的,最后发现原数据库是MYSQL,现在导入的是Oracle数据库,导致SQL语句出现了问题。
手动建表,然后调整SQL语句即可。
显然是数据查出来之后,封装到我们的resultMap或者resultType的时候类型错误了,排查一下SQL语句,一一对照SQL的返回字段以及实体类的属性类型。
比较明显的看出返回的Varchar,但是封装的时候用了Decimal进行接收了,进行修改即可。
显然报的是Oracle的错误,也就是说查询的时候就出了错误,结果都没返回。那么先查看SQL语句拼接可能会出现参数类型转换的地方。
显然找到错误了,原SQL使用的${},会出现SQL注入问题,所以这里进行了修改,通过#{}参数填充来规避掉字符串拼接。但是显然这里的String转换出错了,如下修改即可。
private List<Integer> positionList = Arrays.asList(159,161,174,327,1,366,2);
<if test="positionList != null">
AND device_id in
<foreach collection="positionList" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
一般都是语句有错误,定位上述SelectOnlineCounts,发现缺少了END 关键字
SELECT
SUM(CASE WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END) AS 'count',
SUM(CASE WHEN PORT_IS_ONLN = '是' THEN 1 ELSE 0) AS 'online'
FROM T_V_PASS_ENT_SUMMARY_DATA
修改如下即可:
SELECT
SUM(CASE WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END) AS 'count',
SUM(CASE WHEN PORT_IS_ONLN = '是' THEN 1 ELSE 0 END) AS 'online'
FROM T_V_PASS_ENT_SUMMARY_DATA
SELECT
SUM(CASE WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END) AS 'count',
SUM(CASE WHEN PORT_IS_ONLN = '是' THEN 1 ELSE 0 END) AS 'online'
FROM T_V_PASS_ENT_SUMMARY_DATA
AS 别名,如果别名出现了关键字,就会报这个错,修改别名即可
```java
SELECT
SUM(CASE WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END) AS 'countNumber',
SUM(CASE WHEN PORT_IS_ONLN = '是' THEN 1 ELSE 0 END) AS 'onlineNumber'
FROM T_V_PASS_ENT_SUMMARY_DATA
sum之后,除数为0
SELECT ENT_NM AS name , sum(case WHEN PORT_IS_ONLN = '是' THEN 1 ELSE 0 END) / sum(case WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END) as rate
FROM T_V_PASS_ENT_SUMMARY_DATA
decode(字段或字段的运算,值1,值2,值3)如果字段或者运算是值1就返回值2,否则值3
SELECT ENT_NM AS name , decode(sum(case WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END),0,0,sum(case WHEN PORT_IS_ONLN = '是' THEN 1 ELSE 0 END) / sum(case WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END)) AS rate
FROM T_V_PASS_ENT_SUMMARY_DATA
一般都是SQL语句有问题,发现返回排行前三的数据,where 拼接出现问题了
SELECT rownum ,name,rate from
(SELECT ENT_NM AS name , ROUND(DECODE(SUM(case WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END),0,0,SUM(case WHEN PORT_IS_ONLN = '是' THEN 1 ELSE 0 END) * 100 / SUM(case WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END)),2) AS rate
FROM T_V_PASS_ENT_SUMMARY_DATA
GROUP BY ENT_NM
ORDER BY rate DESC) b
where rownum <![CDATA[ <= ]]> 3
<where>
<if test='distCode != null and distCode != "0"'>
AND DIST_CODE = #{distCode,jdbcType=VARCHAR}
</if>
<if test='startTime != null and endTime != null'>
AND WHSE_DATE BETWEEN #{startTime,jdbcType=DATE} AND #{endTime,jdbcType=DATE}
</if>
<if test='indTp != null and indTp != "0"'>
AND IND_TP = #{indTp,jdbcType=VARCHAR}
</if>
</where>
很明显语序有问题,模糊查询条件放在子查询里
SELECT rownum ,name,rate from
(SELECT ENT_NM AS name , ROUND(DECODE(SUM(case WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END),0,0,SUM(case WHEN PORT_IS_ONLN = '是' THEN 1 ELSE 0 END) * 100 / SUM(case WHEN IS_HAVE_PORT = '是' THEN 1 ELSE 0 END)),2) AS rate
FROM T_V_PASS_ENT_SUMMARY_DATA
<where>
<if test='distCode != null and distCode != "0"'>
AND DIST_CODE = #{distCode,jdbcType=VARCHAR}
</if>
<if test='startTime != null and endTime != null'>
AND WHSE_DATE BETWEEN #{startTime,jdbcType=DATE} AND #{endTime,jdbcType=DATE}
</if>
<if test='indTp != null and indTp != "0"'>
AND IND_TP = #{indTp,jdbcType=VARCHAR}
</if>
</where>
GROUP BY ENT_NM
ORDER BY rate DESC) b
where rownum <![CDATA[ <= ]]> 3
SELECTDIST_NM AS name , SUM(CASE WHEN PORT_IS_ONLN='是' THEN 1 ELSE 0 END) AS ONLINENUMBER
FROM T_V_PASS_ENT_SUMMARY_DATA
显然缺少了空格,修改SQL语句
SELECT DIST_NM AS name , SUM(CASE WHEN PORT_IS_ONLN='是' THEN 1 ELSE 0 END) AS ONLINENUMBER
FROM T_V_PASS_ENT_SUMMARY_DATA
SELECT `TIME`,RS_CO2, RS_CO,RS_NO,RS_HC,OPACITY FROM CORE_DETECT_RESULT
出现字符的地方,只有TIME,去掉符号就好了
SELECT TIME,RS_CO2, RS_CO,RS_NO,RS_HC,OPACITY FROM CORE_DETECT_RESULT
……持续更新