user5181073
user5181073

Reputation:

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

sql.execute("""
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?

Upvotes: 0

Views: 3405

Answers (1)

pgiecek
pgiecek

Reputation: 8200

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)
}

Upvotes: 7

Related Questions