Perform Bulk INSERT, UPDATE, and DELETE Statements

The Go driver supports bulk INSERT, UPDATE, and DELETE statements that have been prepared from a sql.Db* or sql.Tx* object.

Context

Bulk INSERT, UPDATE, and DELETE statements with LOB streaming are not supported.

golang internal processing depends on the dbcapiHDB C library for its implementation, so it tries to batch as many arrays together as possible, up to the array size, based on the types of equal values in consecutive array cases. If types differ in any of the arrays relevant to the first array considered for the batch, the current batch of similar values is sent off first. The execution then continues batching any remaining arrays until all row arrays are inserted. As a result, it is recommended to use the same types for the array values. Note that NULL always matches the previous type considered.

To execute an insert, update, or delete with a batch of parameters, complete the following steps:

Procedure

  1. Prepare the insert/update/delete statement.
    stmt, err := db.Prepare("<INSERT/UPDATE/DELETE statement>")
  2. Create the batch of row values for the insert/update/delete.

    Each row is an array of interface objects ([]interface{}) and the batch is an array of these rows ([][]interface{}).

    The following rules must be followed when creating the batch of rows:
    • Create the batch of rows (type [][]interface{}) with each []interface{} array containing the values for each column of that row. A nil value is interpreted as a NULL for the column.

    • Each row array provided in the batch of values must be the same length, and at least as long as the number of input parameters expected.

    The following input parameter types are supported as row array values for bulk inserts:
    • int8, int16, int32, int64

    • float32, float64

    • bool

    • []byte

    • String

    • time.Time

    • All sql.Null* types

    The following example inserts the values into all columns of a table defined with a column sequence of A INTEGER, B DOUBLE:
    batchSize := 20
    data := make([][]interface{}, batchSize)
    len
    for i := 0; i < batchSize; i++ {
        data[i] = []interface{}{int16(i), float64(i)}
    }
  3. Provide the created data array as a single input parameter to stmt.Exec().
    res, err := stmt.Exec(batchOfRows)