Reputation: 11
I'd like to insert array data equip
to a table using loop.
var svr = 1;
var equip = [3, 4, 5];
For that I need to insert the data three times.
Looks like this:
INSERT INTO khnp.link_server_equipment_map(svr, equip)
VALUES (1, 3);
INSERT INTO khnp.link_server_equipment_map(svr, equip)
VALUES (1, 4);
INSERT INTO khnp.link_server_equipment_map(svr, equip)
VALUES (1, 5);
Can someone please get me out of this rabbit hole? Thanks
Upvotes: 1
Views: 492
Reputation: 839
You can try unnest:
INSERT INTO khnp.link_server_equipment_map(svr, equip)
VALUES (1, UNNEST(ARRAY[3, 4, 5]));`
Upvotes: 2
Reputation: 2493
You can use the INSERT
statement to insert several rows.
INSERT INTO table_name (column_list)
VALUES
(value_list_1),
(value_list_2),
...
(value_list_n);
According to your mentioned data example the rows insertion would be done this way
INSERT INTO khnp.link_server_equipment_map(svr, equip) VALUES
(1, 3),
(1, 4),
(1, 5);
Also to avoid adding the array content one by one you can use the UNNEST
array function.
Upvotes: 0