从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()来获得结果列表。是否有一种更简单的方法来制作行关闭列表?基本上,我需要那里的所有字段都映射到我准备好的语句。

更新:这里是一个避免使用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
    }

我这样做了,所以我没有双重查询:

def resultList = []
row.getMetaData().getColumnCount().times{
    resultList.add(row.getAt(it))
}
resultList.add(row.SUD_ID)
sql.executeUpdate(updateSqlText, resultList)

只是遍历结果集并将其添加到数组中。

最新更新