This must one of the dumbest questions I've ever asked here, but there must something really sick hiding in my SQL script that is blocking it from running.
I'm invoking the cobertura.sql file using the following sample CLI syntax:
psql -h localhost -U myUser -f cobertura.sql myDB
But it complains with the following error:
psql:cobertura.sql:29: ERROR: "sql " is not a known variable LINE 14: sql := format('insert into cobertura_tmp select count(*) as ... cobertura.sql file:
DO language plpgsql $$
declare
eq record;
sql varchar;
BEGIN
create table if not exists cobertura_tmp (num integer, realtime char(1), lat numeric, lng numeric);
truncate table cobertura_tmp;
for eq in select imei_equipo as imei from cliente_avl_equipo where id_cliente in (select id from cliente where nombre ilike '%enangab%') limit 3
loop
sql := format('insert into cobertura_tmp select count(*) as num, tipo as realtime, round(CAST(latitud as numeric), 4) as lat ,round(CAST(longitud as numeric), 4) as lng from reports.avl_historico_%s where latitud between -38.67405472 and -36.75131149 and longitud between -73.08429161 and -69.65333954 group by tipo, round(CAST(latitud as numeric), 4),round(CAST(longitud as numeric), 4)', eq.imei);
execute sql;
end loop;
update cobertura_tmp set num= -1* num where realtime='S';
create table if not exists cobertura_tmp_resumen (num integer, lat numeric, lng numeric);
truncate cobertura_tmp_resumen;
-- select sum(num) as num , lat, lng into cobertura_tmp_resumen from cobertura_tmp group by lat,lng;
-- drop table if exists cobertura_tmp;
END;
$$;
The same script runs remotely and smoothly from Mac OSX using Postico Version 1.3.2 (2318).
QUESTION: Why isn't it finding the sql variable that is in the declare section?