Kettle table input使用变量替换in中的条件

1. 问题描述

今天在使用kettle从PostgreSQL数据库表中导出数据到文本文件时,用到了in查询,原始的sql是下面这样的:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where from_station in ('QTP','CTH','CLP','BDP','BEP','SHD') or to_station in ('QTP','CTH','CLP','BDP','BEP','SHD')
在Kettle的table input中使用变量替换的方式传入from_station和to_station的值,一开始的写法是这样的:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where board_station in (${fromStationCode}) or to_station in (${toStationCode})
执行后发现有语法错误,意识到变量引用应该放到单引号里面,于是,改成了下面这样:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where board_station in ('${fromStationCode}') or to_station in ('${toStationCode}')
这样解析出来的SQL是有问题的,in里面的值最外面多了一层单引号:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where from_station in (''QTP','CTH','CLP','BDP','BEP','SHD'') or to_station in (''QTP','CTH','CLP','BDP','BEP','SHD'')
于是,想到了各种转义,包括在table input步骤的sql中对单引号转义和对参数里面的单引号进行转义,结果都无效。

2. 解决方法

查看Kettle的使用说明,发现变量的引用方式有两种,一种是我们上面使用的${}的方式,这种方式使用的时候外面一般都要套上一层单引号;另外一种是在变量名两边都加上另个%%的方式,这种方式不需要再加单引号,于是,把我们的查询改成了下面这样的:
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS') as checkin_time, '${personType}' as bd_type, 'AA.AA.AA.AA' as checkin_window_ip from compare_result where board_station in (%%fromStationCode%%) or to_station in (%%toStationCode%%)
再次执行Kettle作业,发现作业被成功执行了,问题解决。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值