我有这样的场景:
我需要从select中更新批处理中的某些行,这是我的近似代码
String queryText = "select " + config.fields.join(",") +" from myTable where myfield > 0"
String updateSqlText = "update otherTable set ("+ config.oterhTable.fields.join("=?,") + " ) where ID=?";
// just an example for insert
String insertSqlText = "insert into otherTable ("+ config.oterhTable.fields.join(",") + " ) values (?,?,?,?,?,?,?,?,?,? ...)";
def updateCounts = otherSql.withBatch(100, updateSqlText) { ps ->
riskSql.eachRow(queryText){ row ->
if (row.ID == 0){
// this doesn't work, but I want to do something similar
otherSql.execute(insertSqlText, row)
} else {
// this doesn't work, I need to convert to list. The error I get: aught: java.sql.SQLException: Unable to convert between com.sun.proxy.$Proxy6 and JAVA_OBJECT
ps.addBatch(row, row.SUD_ID)
}
}
}
queryText的查询结果很大,因此我无法调用.rows()来获取结果列表。是否有更简单的方法来列出行闭包?基本上我需要那里的所有字段来映射到我准备好的陈述。
答案 0 :(得分:1)
<强>更新强>
这是一个避免使用eachRow
的解决方案,而是使用带有页面和偏移量的rows
。这应该可以避免大的结果集问题。
def count = sql.firstRow('select count(*) as c from <table>').c
def page = 0
def pageSize = 10
while (page * pageSize < count) {
sql.rows('select * from <table>', page, pageSize).each { GroovyRowResult row ->
def values = row.collect { key, value -> value }
println values // <- values is a List
}
++page
}