0

I am trying to write a conditional batch insert based on Java Parameters, for example

List<Object[]> params = new ArrayList();
params.add(new Object[]{1, 2, 3});

getSimpleJdbcTemplate.batchUpdate(
"INSERT INTO SomeTable( colA, colB, colC)" +
 " VALUES(?, ?, ?)" +
 " WHERE EXISTS ( // some condition )", params);

This obviously does not work, and the closest example I have found involves selecting the INSERT values from tables, instead of the List arguments, which is what I need. How can I reference the Object[] parameters as part of the INSERT values?

Thank you.

4

2 に答える 2

1

この条件を別の SELECT に移動する必要があると思います。

于 2010-11-08T06:43:21.660 に答える
0

例として、次の構文とSimpleJdbcOperations.updateメソッドを試してください。

getSimpleJdbcTemplate.update(
    " INSERT INTO SomeTable( colA, colB, colC)" +
    " SELECT srcA, srcB, srcC" +
    " FROM src_table" +
    " WHERE EXISTS ( // some condition )",
    params
);

さらに、動的 SQL の生成にStringTemplateを使用しています。それは私が今まで使った中で最高の言語です。

例 (StringTemplate 構文として):

StringTemplate tmpl = new StringTemplate(
    " SELECT *" +
    " FROM some_table" +
    " WHERE 1 = 1" +
    "$search_age:{ AND age = ?}$" +
    "$search_name:{ AND name = ?}$"
);


/**
 * if param_age is not null, "AND age = ?" would be included in argumented sql,
 * same as param_name
 */
tmpl.setAttribute("age", param_age);
tmpl.setAttribute("name", param_name);
// :~)

String sqlTmpl = tmpl.toString(); // final SQL generated
于 2010-11-08T07:27:14.610 に答える