How do I use an array to insert to a table in a function?
CREATE TABLE places
(
id SERIAL PRIMARY KEY
,location VARCHAR(100) NOT NULL
,timestamp TIMESTAMP
,person_id INTEGER NOT NULL REFERENCES people ON UPDATE...
);
CREATE TABLE people
(
id SERIAL PRIMARY KEY
,name VARCHAR(100) NOT NULL
);
I am trying to do a function where I would do:
SELECT function(location_name, now(), '{1,2,3}');
This would INSERT 3 records into the places table for each person_id in the array, with the same location and timestamp. The main problem is that the length of the array can be dynamic, so the function should be able to handle:
SELECT function(location_name, now(), '{3,5,7,8,10}');
I don't know where to start in making the INSERT dynamic and extracting the ids from the array. Unest can be used to INSERT INTO variables in DECLARE but the dynamic aspect cannot be done. I hope I made this clear.