主要需求
1、命令行传参
2、批处理文件使用参数
3、循环处理数据(foreach)
调用示例
cd {sql_file_path} && psql -h {host} -p {port} -U {user} -W {password} -d {dbname} -v str_param_name='xxx' -v min_int_param_name=1 -v max_int_param_name=100 -f batch.sql
批处理文件内容示例
({sql_file_path}/batch.sql)
set vars.str_param to :'str_param_name'; -- 字符串参数
set vars.min_int_param to :min_int_param_name; -- 数字参数
set vars.max_int_param to :max_int_param_name; -- 数字参数
DO
$do$
declare
str_val varchar;
str_arr varchar[] := ARRAY(select distinct str_data from conf_table where conf_name=current_setting('vars.str_param') and conf_id between current_setting('vars.min_int_param') and current_setting('vars.max_int_param'))::varchar[];
begin
raise notice 'get str param:%',current_setting('vars.str_param');
raise notice 'get min int param:%',current_setting('vars.min_int_param');
raise notice 'get max int param:%',current_setting('vars.max_int_param');
foreach str_val in array str_arr
loop
-- 数据处理sql
endloop
end
$do$;