dav*_*vid 3 postgresql plpgsql
我有以下地理编码功能,无需该EXCEPTION WHEN OTHERS THEN
部分即可正常工作。但是,我希望循环继续,以防出现异常。所以我试图通过使用来做到这一点EXCEPTION WHEN OTHERS THEN
CREATE OR REPLACE FUNCTION mygetcounty2() RETURNS void LANGUAGE PLPGSQL AS $$
DECLARE idVariable uuid;
begin
FOR i IN 1..100 LOOP
SELECT id into idVariable FROM address_table WHERE county IS NULL AND parse_address_error = false LIMIT 1;
UPDATE address_table set county =
(select namelsad from tiger_data.county_all where ST_Contains( the_geom, ST_GeomFromText( (SELECT ST_AsText( ST_SnapToGrid(g.geomout, 0.00001) ) As wktlonlat FROM geocode(address, 1 ) AS g), 4269 ) ))
where id = idVariable;
EXCEPTION WHEN OTHERS THEN
UPDATE address_table set parse_address_error = true where id = idVariable;
END LOOP;
return;
end;
$$;
Run Code Online (Sandbox Code Playgroud)
但在尝试创建该函数时我收到以下消息
ERROR: syntax error at or near "EXCEPTION"
LINE 15: EXCEPTION WHEN OTHERS THEN
^
SQL state: 42601
Character: 511
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
编辑:
我在循环内添加了一个新的begin
,块。end
创建函数时我不再遇到错误。
Create or replace function mygetcounty2()
returns void
language plpgsql
as
$$
DECLARE idVariable uuid;
begin
FOR i IN 1..100 LOOP
begin
SELECT id into idVariable FROM address_table WHERE county IS NULL AND parse_address_error = false LIMIT 1;
UPDATE address_table set county =
(select namelsad from tiger_data.county_all where ST_Contains( the_geom, ST_GeomFromText( (SELECT ST_AsText( ST_SnapToGrid(g.geomout, 0.00001) ) As wktlonlat FROM geocode(address, 1 ) AS g), 4269 ) ))
where id = idVariable;
EXCEPTION WHEN OTHERS THEN
raise notice 'oops %', sqlstate;
UPDATE address_table set parse_address_error = true where id = idVariable;
end;
END LOOP;
return;
end;
$$;
Run Code Online (Sandbox Code Playgroud)