人大金仓 dateAdd函数使用

发布时间 2024-01-12 16:12:19作者: 张三01011010

坚持把简单的事情做好,就是不简单。

xxl-job适配出现问题,报错信息:

org.postgresql.util.PSQLException: ERROR: syntax error at or near “SECOND”

这是因为人大金仓默认不支持DATE_ADD()函数

首先需要使用SYSTEM账号执行语句 create extension kdb_date_function; 创建扩展

然后对sql还要进行修改,这里参考了Oracle的函数使用(同事大佬提醒可以参考Oracle的函数写法)

一开始是这样写的:

SELECT t.id
FROM xxl_job_registry AS t
WHERE t.update_time <![CDATA[ < ]]> DATE_ADD(#{nowTime},INTERVAL -#{timeout} SECOND)

按照这样的写法,在金仓数据库里,sql应该是这样写的:

seleat * from nrod vv1 oh vvi toh recistrn
where update time < date_add ('2024-01-12 14:53:30.826',INTERVAL '-2' second)

也就是说,#{timeout}这个参数需要用单引号包裹起来,变成 '#{timeout}',这样做就会导致另外一个问题:

参数无法替换,timeout的值传不进来,就会报错:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMappingfpro

perty='timeout', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause
org apache.ibatis type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: com.kingbase8.util.KsQLException: The column index is out of range: 2, number of

img

解决方案:使用oracle的dateadd函数来写:

DATEADD(INTERVAL,timeout,nowtime)

SELECT t.id
FROM xxl_job_registry AS t
WHERE t.update_time <![CDATA[ < ]]> DATEADD(SECOND,-#{timeout},#{nowTime})

参数解释一下:

  1. INTERVAL就是你想计算的时间单位(hour、second、minute)
  2. timeout是一个integer,你想查询的时间段
  3. nowtime是一个date类型的数据

比如我这个sql是想查询小于从当前时间(nowTime)到某个时间之前(timeout)的数据,写出来sql就是查询从当前时间的九十秒之前的数据

select * from xxl_job_repositry 
where update_time < dateadd(second,90,'2024-01-12:12:21:30:654')