简体   繁体   English

在Groovy中的一条SQL语句中插入多行

[英]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? 使用groovy的预处理语句是否有可能? 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 但这会插入第一个SKU的3倍,即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. 您可以使用withBatch(String, Closure)方法在批处理中执行关闭(包含特定于关联的预处理语句的批处理操作)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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