Here's my Stored Procedure:
CREATE OR REPLACE FUNCTION save_array(arraynumbers integer[], arraystrings text[])
RETURNS void AS $$
BEGIN
INSERT INTO employees (id, name)
VALUES (arraynumbers[0], arraystrings[0]),
(arraynumbers[1], arraystrings[1]),
(arraynumbers[2], arraystrings[2])
END;
$$
LANGUAGE PLPGSQL;
How do I implement a loop to save each index of the arrays as a new record?
You can use an insert ... select
together with unnest()
. And you don't need PL/pgSQL for that:
CREATE OR REPLACE FUNCTION save_array(arraynumbers integer[], arraystrings text[])
RETURNS void
AS $$
insert into employees(id, name)
select unnest(arraynumbers), unnest(arraystrings);
$$
LANGUAGE sql;