简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM