J.Doe J.Doe - 9 months ago 69
SQL Question

Insert multiple rows in one SQL statement in Groovy

I need to insert multiple values in one statement. Is this possible using groovy's prepared statements? I have the following

insert into all_priceuploaddummy (seller_sku) values (?.sku), (?.sku), (?.sku)
, [[sku:1],[sku:2],[sku:3]])

But that inserts 3 times the first sku, 1

How can I do that without looping over all the entries?


You can use withBatch(String, Closure) method that performs the closure (containing batch operations specific to an associated prepared statement) within a batch.

def updateCounts = sql.withBatch('insert into TABLENAME(a, b, c) values (?, ?, ?)') { ps ->
     ps.addBatch([1, 2, 3])
     ps.addBatch([10, 20, 30])
     ps.addBatch(100, 200, 300)