DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null

今天不得不吐槽下 DB2了,不过也不定是因为DB2,因为之前写了一段sql语句能执行,但是最近发现又报错,

错误代码-302,很奇怪,在网上搜索了差不多一天都是说“sql中要更新的某个字段的长度超过了数据库表中字段的定义长度”!

但是和这个也不沾边啊,我写的是查询语句,既然这样我就重新写了sql语句,用了不同的函数达到同样的需求,也没想到,

真没想到竟然成功了!!!

下面是我的部分sql语句:

select a.act_consume_qty,a.act_amount,b.output_qty,c.budget_consume_qty,c.budget_amount,d.budget_output_qty
		from (  select nvl(sum(act_consume_qty),0) as act_consume_qty,nvl(sum(act_amount),0) as act_amount
		from tmmicsalj01
		where (prod_unit = #prodUnit# or (#prodUnit# = '’ and prod_unit not like 'A%'))
		and prod_date BETWEEN  #dateTimeStart# AND #dateTimeEnd# ) a,

  以前where后

(prod_unit = #prodUnit# or (#prodUnit# = '' and prod_unit not like 'A%'))
		and prod_date BETWEEN  #dateTimeStart# AND #dateTimeEnd# 

的语句中#prodUnit# = ''当prodUnit=''时没问题,但prodUnit不为空字符串时,就会报错。

修改之后的语句:
select a.act_consume_qty,a.act_amount,b.output_qty,c.budget_consume_qty,c.budget_amount,d.budget_output_qty
		from (  select nvl(sum(act_consume_qty),0) as act_consume_qty,nvl(sum(act_amount),0) as act_amount
		from tmmicsalj01
		where (prod_unit = #prodUnit# or (LENGTH(TRIM(#prodUnit#)) = 0 and prod_unit not like 'A%'))
		AND(MAT_CODE = #rawProdCode# OR (LENGTH(TRIM(#rawProdCode#)) = 0 AND MAT_CODE LIKE 'C%'))
		and prod_date BETWEEN  #dateTimeStart# AND #dateTimeEnd# ) a,

  把#prodUnit# = ' '换成prod_unit = #prodUnit# or (LENGTH(TRIM(#prodUnit#)) = 0 and prod_unit not like 'A%')就没任何问题了!

虽然是解决了问题,但还是没弄明白是什么原因导致这个问题发生的,还是掌握的知识太少啊。。。

上一篇:关于GitHub与Hexo搭建博客


下一篇:Hexo 搭建:搭建与配置,arm架构linux