在Groovy中的一条SQL语句中插入多行 [英] Insert multiple rows in one SQL statement in Groovy

查看:95
本文介绍了在Groovy中的一条SQL语句中插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个语句中插入多个值.使用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]])

但这会插入第一个sku的3倍,即1

But that inserts 3 times the first sku, 1

如何做到这一点而又不遍历所有条目?

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

推荐答案

您可以使用 withBatch(String,Closure)方法在内部执行关闭(包含特定于关联的准备好的语句的批处理操作)一批.

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

这篇关于在Groovy中的一条SQL语句中插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆