2017-03-01 117 views
0

我创建了一个动态函数。我动态地获取表名的一部分。该功能已成功创建。但是当我执行该功能。我收到一个错误。我怎么解决这个问题?我打电话跟Postgresql函数动态表quote_ident错误

select * from dwgcould.getlatlngcenter(2000653); 


CREATE OR REPLACE FUNCTION dwgcould.getlatlngcenter(IN pro_id integer, 
    OUT lat_center double precision, OUT lng_center double precision) 
AS $$ 

BEGIN 
     EXECUTE 'SELECT st_x(st_centroid(st_transform(geom,4326))) as lng_center ,st_y(st_centroid(st_transform(geom,4326))) as lat_center 
     FROM dwgcould.adpes_v1_' || quote_ident(pro_id) || '_line limit 1'; 
END; 
$$ LANGUAGE plpgsql; 

功能的错误代码是

ERROR: function quote_ident(integer) does not exist LINE 2: FROM dwgcould.adpes_v1_' || quote_ident(pro_id) || '_line... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. QUERY: SELECT 'SELECT st_x(st_centroid(st_transform(geom,4326))) as lng_center ,st_y(st_centroid(st_transform(geom,4326))) as lat_center FROM dwgcould.adpes_v1_' || quote_ident(pro_id) || '_line limit 1' CONTEXT: PL/pgSQL function dwgcould.getlatlngcenter(integer) line 4 at EXECUTE statement ********** Error ********** ERROR: function quote_ident(integer) does not exist SQL state: 42883 Hint: No function matches the given name and argument types. You might need to add explicit type casts. Context: PL/pgSQL function dwgcould.getlatlngcenter(integer) line 4 at EXECUTE statement

此外,我如何检查表是否存在?

+0

'如果(选择从pg_tables计数(1)其中,表名= 'TN')<1然后...' –

回答

0

更好地使用的格式,例如:

CREATE OR REPLACE FUNCTION dwgcould.getlatlngcenter(IN pro_id integer, 
    OUT lat_center double precision, OUT lng_center double precision) 
AS $$ 

BEGIN 
    if (select count(1) from pg_tables where tablename = format('adpes_v1_%s_line',pro_id)) < 1 then 
    raise info '%','NO SUCH TABLE!'; 
    return; 
    end if; 
    EXECUTE format('SELECT * FROM dwgcould.adpes_v1_%s_line limit 1',pro_id) into lat_center,lng_center; 
    return; 
END; 
$$ LANGUAGE plpgsql; 

docs

+0

它是工作,但我获得空值。如果我直接执行SELECT ST_X(st_centroid(st_transform(的geom,4326)))作为lng_center,st_y(st_centroid(st_transform(的geom,4326)))作为lat_center \t \t FROM dwgcould.adpes_v1_2000653_line极限1 I得到的结果。 – Alexis

+0

加入'lat_center,lng_center;'执行后 –

+0

非常感谢你的工作:) – Alexis