DB2错误。通过选择查询插入表

时间:2017-04-20 17:31:45

标签: sql db2

我试着在使用表和子查询的查询中向表中插入一些数据。子查询是使用case子句的地方,我得到错误:ERROR [HY000] [IBM] [Controlador ODBC de iSeries Access] [DB2 UDB] SQL0420 - CAST参数字符无效。

运行insert命令时,我只收到此错误。当我运行select命令时没有错误。这是查询

insert into qlib.table 
select a.fieldkey, a.field2, ifnull(n.firstdate,0) as firstdate, ifnull(n.lastdate,0) as lastdate, n.totamount, n.timespan
from mainlib.tablea a
left join (select b.fieldkey, MIN(rtrim(b.year)||'-'||case when b.month<10 then '0'||b.month else rtrim(b.month)end) as firstdate, 
              MAX(rtrim(b.year)||'-'||case when b.month<10 then '0'||b.month else rtrim(b.month) end) as lastdate, sum(b.amount) as totamount, 
                  cast(count(b.year)/12 as char(2))||' Years '||cast(count(b.year)-(count(b.year)/12)*12 as char(2))||' Months' as  timespan
           from mainlib.tableb b
           group by b.fieldkey) n on n.fieldkey = a.fieldkey 

Tableb包含每月节省的确定。子查询返回保存的第一年 - 月份,上一个月,总金额,以及每月保存的年和月数,假设每个月都有一个保存/存款,每个字段密钥

子查询的强制转换子句有什么问题吗?当我尝试使用select命令插入qlib.table时,我只会遇到错误。

1 个答案:

答案 0 :(得分:0)

有些事情可能会清理您的查询:

case when b.month<10 then '0'||b.month else rtrim(b.month)end可以替换为
VARCHAR_FORMAT(b.month,'00')

cast(count(b.year)/12 as char(2))||' Years '||cast(count(b.year)-(count(b.year)/12)*12 as char(2))||' Months'改写为

VARCHAR_FORMAT(count(b.year)/12,'99')||' Years '||VARCHAR_FORMAT(count(b.year)-(count(b.year)/12)*12,'99')||' Months'