使用postgresql替换查询中的值

时间:2017-04-17 03:16:44

标签: sql postgresql

我在postgresql上有这个查询:

COPY (SELECT 
"UR_16PIX_Chih"."ID_UR", 
"UR_16PIX_Chih"."IDEDOMUN15", 
"UR_16PIX_Chih"."PIX1", 
"UR_16PIX_Chih"."PIX2", 
"UR_16PIX_Chih"."PIX3", 
"UR_16PIX_Chih"."PIX4", 
"UR_16PIX_Chih"."PIX5", 
"UR_16PIX_Chih"."PIX6", 
"UR_16PIX_Chih"."PIX7", 
"UR_16PIX_Chih"."PIX8", 
"UR_16PIX_Chih"."PIX9", 
"UR_16PIX_Chih"."PIX10", 
"UR_16PIX_Chih"."PIX11", 
"UR_16PIX_Chih"."PIX12", 
"UR_16PIX_Chih"."PIX13", 
"UR_16PIX_Chih"."PIX14", 
"UR_16PIX_Chih"."PIX15", 
"UR_16PIX_Chih"."PIX16" 
FROM 
public."UR_16PIX_Chih"
WHERE 
"UR_16PIX_Chih"."IDEDOMUN15" = '08061') TO 
'/home/manager/data/Chihuahua/08061.csv' WITH CSV HEADER;

我想知道如何将'08061'替换为'08062',接着替换'08063',接下来替换为'08064',接下来替换为'08065'等等......使用自动方式。同时我只是编辑查询用pgadmin sql编辑器中的下一个值替换'08061'值,但我需要一种自动执行此操作的方法。

1 个答案:

答案 0 :(得分:0)

一个选项是create or replace function c() returns void as $c$ declare r record; begin for r in select distinct "IDEDOMUN15" as i from public."UR_16PIX_Chih" loop execute format($$ copy ( select "ID_UR", ... from public."UR_16PIX_Chih" where "IDEDOMUN15" = '%1$s' ) to '/var/lib/pgsql/%1$s.csv' $$, r.i); end loop; end; $c$ language plpgsql; 功能:

a