> ## Content Index
> Fetch the complete content index at: https://www.saraburke.net/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Load CSVs With SQL Subqueries in Liquibase
- URL: https://www.saraburke.net/how-to-load-csvs-with-subqueries-in-liquibase/
- Published: 2023-04-18T21:08:33.000Z
- Updated: 2023-04-18T21:08:33.000Z
- Author: Sara Burke
- Tags: database, software

Say you want to set up a [Liquibase](https://www.liquibase.org/?ref=saraburke.net) changelog to insert some data, but you don't know the IDs of some entity, and you can express it as a query. With Liquibase's `computed` column attribute, you can embed subqueries directly into seed data:

```
column1;column2
(select id from table2 where some_column = 'foo');BAR
(select id from table2 where some_column = 'foo');BAZ
```

The parentheses are important!

Mind the parentheses! Liquibase converts a `loadData` change into an `INSERT INTO ... VALUES (...)` statement, so your subqueries need to be tucked in and cozy.

Your changelog should look something like this:

```
databaseChangeLog:
  - changeSet:
      id: 1
      author: xxx
      objectQuotingStrategy: QUOTE_ONLY_RESERVED_WORDS
      preConditions:
        - onFail: MARK_RAN
        - sqlCheck:
            expectedResult: 0
            sql: select count(*) from my_table
      changes:
        - loadData:
            tableName: my_table
            file: db/seeds/my_table.csv
            separator: ;
            encoding: UTF-8
            columns:
              - column:
                  header: column1
                  name: column1
                  computed: true
              - column:
                  header: column2
                  name: column2
                  computed: false

```

It's usually a good idea to use preconditions when you're modifying data in a table.

And that's it! Let me know if this is useful to you! Liquibase's documentation is pretty rough in parts, and it took me a little bit to figure out how to assemble these pieces into a solution to my problem.