postgresql动态函数
postgresql dynamic function
我正在用参数替换硬编码名称,需要创建一个动态 sql 字符串。不管我做什么,我都无法在子查询中替换 'param'、'schema' 和 'tablename_2'。接下来是我的功能,因为它正在部分工作,然后是我的错误消息。我认为这只是在正确位置插入正确引号的问题。任何帮助将不胜感激。
CREATE OR REPLACE FUNCTION public.function_112(schema text, tablename_1 text, tablename_2 text, param integer)
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
EXECUTE 'INSERT INTO ' || quote_ident(schema) || '.' || quote_ident(tablename_1) ||'
("gid", "osm_id", "name", "geom", "line_type")
SELECT ("gid", "osm_id", "name", ST_SetSRID("geom", 4326), param)
FROM ''|| quote_ident(schema) || ''.'' || quote_ident(tablename_2) ||'';'
RETURN;
END
$function$
--> 导致错误消息:
QUERY: INSERT INTO public."National_Grid_multiline"
("gid", "osm_id", "name", "geom", "line_type")
SELECT ("gid", "osm_id", "name", ST_SetSRID("geom", 4326), param)
FROM '|| quote_ident(schema) || '.' || quote_ident(tablename_2) ||';
对于那些感兴趣的人,这是正确的解决方案:
CREATE OR REPLACE FUNCTION public.afri_terra_112(schema text, tablename_1 text, tablename_2 text, param integer)
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
EXECUTE 'INSERT INTO ' || quote_ident(schema) || '.' || quote_ident(tablename_1) ||
' ("gid", "osm_id", "name", "geom", "line_type") '
' SELECT "gid", "osm_id", "name", ST_SetSRID("geom", 4326), '|| param ||' '
' FROM ' || quote_ident(schema) || '."'|| quote_ident(tablename_2) ||'"';
RETURN;
END
$function$
我正在用参数替换硬编码名称,需要创建一个动态 sql 字符串。不管我做什么,我都无法在子查询中替换 'param'、'schema' 和 'tablename_2'。接下来是我的功能,因为它正在部分工作,然后是我的错误消息。我认为这只是在正确位置插入正确引号的问题。任何帮助将不胜感激。
CREATE OR REPLACE FUNCTION public.function_112(schema text, tablename_1 text, tablename_2 text, param integer)
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
EXECUTE 'INSERT INTO ' || quote_ident(schema) || '.' || quote_ident(tablename_1) ||'
("gid", "osm_id", "name", "geom", "line_type")
SELECT ("gid", "osm_id", "name", ST_SetSRID("geom", 4326), param)
FROM ''|| quote_ident(schema) || ''.'' || quote_ident(tablename_2) ||'';'
RETURN;
END
$function$
--> 导致错误消息:
QUERY: INSERT INTO public."National_Grid_multiline"
("gid", "osm_id", "name", "geom", "line_type")
SELECT ("gid", "osm_id", "name", ST_SetSRID("geom", 4326), param)
FROM '|| quote_ident(schema) || '.' || quote_ident(tablename_2) ||';
对于那些感兴趣的人,这是正确的解决方案:
CREATE OR REPLACE FUNCTION public.afri_terra_112(schema text, tablename_1 text, tablename_2 text, param integer)
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
EXECUTE 'INSERT INTO ' || quote_ident(schema) || '.' || quote_ident(tablename_1) ||
' ("gid", "osm_id", "name", "geom", "line_type") '
' SELECT "gid", "osm_id", "name", ST_SetSRID("geom", 4326), '|| param ||' '
' FROM ' || quote_ident(schema) || '."'|| quote_ident(tablename_2) ||'"';
RETURN;
END
$function$