plpgsql に一時テーブルを作成する関数があり、ループがあります。問題は、ループするたびに、一時テーブルを作成する部分も実行されるため、エラーがポップアップ表示されることです。
ERROR: relation "tmpr" already exists CONTEXT: SQL statement "CREATE TEMPORARY TABLE tmpr ( id int, source geometry, target geometry, dist_ft character varying )"
コードの一部が複数回実行されるのを防ぐ方法はありますか?
以下にコードを示します。
DECLARE
_r record;
t record;
i int := 0;
j int := 1;
count int := 0;
source_geom character varying;
target_geom character varying;
BEGIN
BEGIN
CREATE TEMPORARY TABLE tmpr (
id int,
source geometry,
target geometry,
dist_ft character varying
);
END;
BEGIN
CREATE TEMPORARY TABLE tmp (
ogc_fid int,
wkb_geometry character varying,
track_fid int
);
END;
-- END IF;
WHILE i < 3 --DEPENDS ON THE NUMBER OF TRACKS
LOOP
--j := 1;
--WHILE j < 29 --DEPENDS ON THE NUMBER OF TRACK POINTS
--LOOP
EXECUTE 'INSERT INTO tmp (ogc_fid, wkb_geometry, track_fid)
SELECT '|| quote_ident(gid_cname) ||' , ' ||quote_ident(geo_cname)||' , ' || quote_ident(tid_cname) ||'
FROM ' ||quote_ident(geom_table)|| '
WHERE ' ||quote_ident(tid_cname)|| ' = ' || i;
FOR _r IN EXECUTE
' SELECT *'
||' FROM tmp'
LOOP
EXECUTE 'INSERT INTO tmpr (id, source, target, dist_ft)
SELECT a.'|| quote_ident(gid_cname) || ' AS id,'
|| ' st_astext( a.'||quote_ident(geo_cname)||') AS source,'
|| ' st_astext(b.'||quote_ident(geo_cname)||') AS target, '
|| ' ST_Distance(a.'||quote_ident(geo_cname) || ' , b.'||quote_ident(geo_cname)||') As dist_ft '
|| ' FROM tmp AS a INNER JOIN tmp As b ON ST_DWithin(a.'||quote_ident(geo_cname)|| ', b.'||quote_ident(geo_cname)|| ',1000)'
|| ' WHERE b.'||quote_ident(gid_cname)|| ' > a.'||quote_ident(gid_cname)|| ' AND b.'||quote_ident(tid_cname)|| ' = '||i|| 'AND a.'||quote_ident(tid_cname)|| ' = '||i||
' ORDER BY dist_ft '
|| ' Limit 1 ';
--source_geom := temp.source;
--target_geom := temp.target;
EXECUTE 'update ' || quote_ident(geom_table) ||
' SET source = tmpr.source
, target = tmpr.target
FROM tmpr
WHERE ' || quote_ident(gid_cname) || ' = tmpr.id';
EXECUTE 'delete from tmpr';
END LOOP;
--j = j + 1;
--END LOOP;
EXECUTE 'delete from tmp';
i = i + 1;
END LOOP;
RETURN 'OK';
END;