Liquibase® version 3.8.5 is now available! Get it for free.
2018 XML Announcement
News All Previous Posts >>

Subscribe for email updates

- and/or -

Change: ‘loadUpdateData’

Loads or updates data from a CSV file into an existing table. Differs from loadData by issuing a SQL batch that checks for the existence of a record. If found, the record is UPDATEd, else the record is INSERTed. Also, generates DELETE statements for a rollback.

A value of NULL in a cell will be converted to a database NULL rather than the string ‘NULL’

Available Attributes

NameDescription
catalogNameName of the catalog@ v3.0E.g. 'cat'
commentLineStartsWithLines starting with this character are treated as comment and ignored.Default: '#'
encodingEncoding of the CSV file (defaults to UTF-8)Default: 'utf-8'
fileCSV file to loadE.g. 'com/example/users.csv'
onlyUpdatebooleanIf true, records with no matching database record should be ignored@ v3.3
primaryKeyComma delimited list of the columns for the primary keyE.g. 'pk_id'
quotcharThe quote character for string fields containing the separator character.Default: '"'
relativeToChangelogFilebooleanWhether the file path is relative to the root changelog file rather than to the classpath.
schemaNameName of the schemaE.g. 'public'
separatorCharacter separating the fields.Default: ','
tableNameName of the table to insert or update data inE.g. 'person'
usePreparedStatementsbooleanUse prepared statements instead of insert statement strings if the DB supports it

Nested Properties

NameDescription
columns / column [0..N]CSV -> table column mapping can be defined. Either the 'header' or 'index' attribute needs to be defined for columns if the header name in the CSV is different than the column needs to be inserted If no `column` defined at all, header names has to match the column names in the table.The column type it is taken from the DB. Otherwise for non-string columns the type definition might be requiredNote: columns tag not required in XML
Attributes
name Name of the column.
type Data type of the column. Its value has to be one of the LOAD_DATA_TYPE
header Name of the column in the CSV file from which the value for the column shall be taken if it's different from the column name.
Note: Ignored if index is also defined.
index integer Index of the column in the CSV file from which the value for the column shall be taken
<changeSet author="liquibase-docs" id="loadUpdateData-example">
    <loadUpdateData catalogName="cat"
            commentLineStartsWith="/"
            encoding="UTF-8"
            file="com/example/users.csv"
            onlyUpdate="true"
            primaryKey="pk_id"
            quotchar="'"
            relativeToChangelogFile="true"
            schemaName="public"
            separator=";"
            tableName="person"
            usePreparedStatements="true">
        <column header="header1"
                name="id"
                type="NUMERIC"/>
        <column index="3"
                name="name"
                type="BOOLEAN"/>
    </loadUpdateData>
</changeSet>
changeSet:
  id: loadUpdateData-example
  author: liquibase-docs
  changes:
  - loadUpdateData:
      catalogName: cat
      columns:
      - column:
          header: header1
          name: id
          type: NUMERIC
      - column:
          index: 3
          name: name
          type: BOOLEAN
      commentLineStartsWith: /
      encoding: UTF-8
      file: com/example/users.csv
      onlyUpdate: true
      primaryKey: pk_id
      quotchar: ''''
      relativeToChangelogFile: true
      schemaName: public
      separator: ;
      tableName: person
      usePreparedStatements: true
{
  "changeSet": {
    "id": "loadUpdateData-example",
    "author": "liquibase-docs",
    "changes": [
      {
        "loadUpdateData": {
          "catalogName": "cat",
          "columns": [
            {
              "column": {
                "header": "header1",
                "name": "id",
                "type": "NUMERIC"
              }
            },
            {
              "column": {
                "index": 3,
                "name": "name",
                "type": "BOOLEAN"
              }
            }]
          ,
          "commentLineStartsWith": "/",
          "encoding": "UTF-8",
          "file": "com/example/users.csv",
          "onlyUpdate": true,
          "primaryKey": "pk_id",
          "quotchar": "'",
          "relativeToChangelogFile": true,
          "schemaName": "public",
          "separator": ";",
          "tableName": "person",
          "usePreparedStatements": true
        }
      }]
    
  }
}

Database Support

DatabaseNotesAuto Rollback
DB2/LUWSupportedNo
DB2/zSupportedNo
DerbySupportedNo
FirebirdSupportedNo
H2SupportedNo
HyperSQLSupportedNo
INGRESSupportedNo
InformixSupportedNo
MariaDBSupportedNo
MySQLSupportedNo
OracleSupportedNo
PostgreSQLSupportedNo
SQL ServerSupportedNo
SQLiteSupportedNo
SybaseSupportedNo
Sybase AnywhereSupportedNo