I'm trying to use the multi row insert with postgres
INSERT INTO table_name (column_list)
VALUES
(value_list_1),
(value_list_2),
...
(value_list_n);
on a list of Strings that can of course be of variable length.
How can I use this with a prepared statement?
I'm trying to use the multi row insert with postgres
INSERT INTO table_name (column_list)
VALUES
(value_list_1),
(value_list_2),
...
(value_list_n);
on a list of Strings that can of course be of variable length.
How can I use this with a prepared statement?
Share Improve this question asked 2 days ago Somaiah KumberaSomaiah Kumbera 7,5394 gold badges46 silver badges48 bronze badges1 Answer
Reset to default 4The answer is with Postgres unnest
. This is postgres proprietary so it unfortunately won't work with all SQL:
val ssnList: List<String> = listOf("1", "2")
conn.prepareStatement("insert into ssns values (unnest(?))").use { stm ->
val array = conn.createArrayOf("VARCHAR", ssnList.toTypedArray())
stm.setArray(1, array)
stm.execute()
}