0

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?

1 Answer 1

7

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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.