Insert CLOB into Oracle database

后端 未结 6 2249
孤独总比滥情好
孤独总比滥情好 2020-12-03 16:32

My question is: How do you get around the ORA-01704: string literal too long error when inserting (or doing anything in queries) with CLO

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 16:47

    You are making it way to complicated.

    Use a PreparedStatement and addBatch() for each clob in your list:

    String sql = "insert  into " + tempTableName + " values (?)";
    PreparedStatement stmt = connection.prepareStatement(sql);
    for (String query : readQueries) {
      stmt.setCharacterStream(1, new StringReader(query), query.lenght());
      stmt.addBatch();
    }
    stmt.exececuteBatch();
    

    No messing around with escaping strings, no problem with the length of the literals, no need to create temporary clobs. And most probably just as fast as using a single INSERT ALL statement.

    If you are using a current driver (> 10.2) then I think the setCharacterStream() call and the creation of the Reader is not necessary either. A simple setString(1, query) will most probably work as well.

提交回复
热议问题