Problemas para migrar tablas de bases de datos con groovy sql

I want to select data from one table and insert into another in batches, using different sql connections. The two tables are set up exactly the same. At the moment I have:

destination.withBatch(1000) { stmt ->

    source.eachRow(selectQuery) {

        String insertString = """
                    INSERT INTO dest_table
                    VALUES (
                          ${it[0]},
                          ${it[1]});
                    """

        try {
            stmt.addBatch(insertString)
        }
        catch (Exception e) {
            println insertString
        }
    }
}

Something seems to happen to the data types in this process, because it gets very unhappy inserting a string like 'a:string' because of the colon.

Yo podría hacer '${it[0]}' to enforce it is treated as a String, but this will cause problems when I come to other data types.

Furthermore, my error handling is definitely not working correctly. I want it to print out the inserts that it was unable to execute, and then carry on gracefully.

Muchas Gracias

preguntado el 01 de febrero de 12 a las 14:02

Cual es tu selectQuery and what is the actual Exception? -

1 Respuestas

It's likely that groovy sql is creating a prepared statement from your sql string, and that anything with a colon in it is parsed as a parameter placeholder.

So, I'd suggest going with the flow, and binding your data values separately, instead of putting them inline in the sql statement. This will also probably improve performance, as the prepared statements can be cached by the database.

Respondido 25 Feb 12, 23:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.