I have an SQL function:
-- Function: insert_into_wgs()
-- DROP FUNCTION insert_into_wgs();
CREATE OR REPLACE FUNCTION insert_into_wgs()
RETURNS void AS
$BODY$
BEGIN
INSERT INTO parcels (num,vid,support_num,name_dispatcher,cadastr,the_geom,status_id)
SELECT num,vid,support_num,name_dispatcher,cadastr,ST_TRANSFORM(the_geom,4326),status_id
FROM parcels_temp
WHERE num NOT IN (SELECT num FROM parcels)AND deleted='no';
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION insert_into_wgs() OWNER TO postgres;
You see that it works only for parcels, parcels_temp tables. How can I create function with parameters insert_into_wgs(table, table_temp);?
As mentioned in the comment, you have to use dynamic SQL if you want to parametrize identifiers. And use
EXECUTEin plpgsql.In the base query better use
NOT EXISTSinstead of colNOT IN (<subquery>).Now, if only the table names change and the columns stay the same, this simple demo would do the job:
Check out
format()in the manual.There are quite a few related answers on SO. Try this search.