I have this query at 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;
I want to know How I could replace the value '08061' with '08062', next with '08063', next with '08064', next with '08065' and so... using an automatic way. Meanwhile I'm just editing the query replacing the '08061' value with next values in pgadmin sql editor but I need a way for doing so automatically.
One option is a PL/pgSQL function:
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;