# Pass a variable to an SQL changelog at runtime

**URL:** <https://forum.liquibase.org/t/pass-a-variable-to-an-sql-changelog-at-runtime/9013>\
**Category:** General Discussion\
**Created:** [January 17, 2024, 3:46pm UTC](https://forum.liquibase.org/t/pass-a-variable-to-an-sql-changelog-at-runtime/9013 "2024-01-17T15:46:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![balistico](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@balistico](https://forum.liquibase.org/u/balistico)\
**Post date:** [January 17, 2024, 3:46pm UTC](https://forum.liquibase.org/t/pass-a-variable-to-an-sql-changelog-at-runtime/9013/1 "2024-01-17T15:46:12Z")

</div>

Hi, I’m using the Liquibase Java API (version 4.25.1) for change management. In my SQL changelog, I have placeholders for specific properties, like so:

```auto
CREATE SCHEMA IF NOT EXISTS ${schemaName};

```

How can I, through the Liquibase Java API, make the desired properties available for use? Is this even possible? I have not seen any examples for the API, only for the CLI.

Below is my sample changelog and API code:

```auto
--liquibase formatted sql

--changeset dm:create-schema-sample context:"dev" failOnError:true
CREATE SCHEMA IF NOT EXISTS ${schemaName};

```

```auto
try (Connection connection = DriverManager.getConnection(url, user, password)) {
    Map<String, Object> config = new HashMap<>();
    Scope.child(config, () -> {
        JdbcConnection jdbcConnection = new JdbcConnection(connection);
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
        CommandScope commandScope = new CommandScope(COMMAND_NAME);
        commandScope.addArgumentValue(CONTEXTS_ARG, "dev");
        commandScope.addArgumentValue(CHANGELOG_FILE_ARG, "/database/changelog.sql");
        commandScope.addArgumentValue(DATABASE_ARG, database);
        commandScope.execute();
    });
} catch (Exception e) {
    throw new RuntimeException(e);
}

```

I have tried a few different approaches, but the placeholder is never replaced by the actual property.

---

<div class="post-metadata">

**Author:** ![wwillard](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wwillard](https://forum.liquibase.org/u/wwillard)\
**Post date:** [January 18, 2024, 8:16pm UTC](https://forum.liquibase.org/t/pass-a-variable-to-an-sql-changelog-at-runtime/9013/2 "2024-01-18T20:16:49Z")

</div>

You can pass the property by specifying an environment variable or a Java system property.

This works for me:

` java -DschemaName=NewSchema -classpath .:liquibase-core.jar:postgresql-42.0.6.jar TestClass`

`schemaName=MY_SCHEMA java -classpath .:liquibase-core.jar:postgresql-42.0.6.jar TestClass`

---

<div class="post-metadata">

**Author:** ![balistico](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@balistico](https://forum.liquibase.org/u/balistico)\
**Post date:** [January 19, 2024, 11:47am UTC](https://forum.liquibase.org/t/pass-a-variable-to-an-sql-changelog-at-runtime/9013/3 "2024-01-19T11:47:11Z")

</div>

Thanks for your answer. I’m running the code from AWS in a Lambda function, so unfortunately I can’t use your suggestions. Properties would have to be passed on in some other way, specifically through the Java API

---

<div class="post-metadata">

**Author:** ![wwillard](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wwillard](https://forum.liquibase.org/u/wwillard)\
**Post date:** [January 19, 2024, 12:50pm UTC](https://forum.liquibase.org/t/pass-a-variable-to-an-sql-changelog-at-runtime/9013/4 "2024-01-19T12:50:41Z")

</div>

Another option is to use the `ChangeLogParameters` object. You can create an instance of this type, set your property, and then pass it to the update command, like this:

```auto

        Scope.child(config, () -> {
            JdbcConnection jdbcConnection = new JdbcConnection(connection);
            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
            ChangeLogParameters changelogParameters = new ChangeLogParameters(database);
            changelogParameters.set("schemaName", "newSchema");
            CommandScope commandScope = new CommandScope(UpdateCommandStep.COMMAND_NAME);
            commandScope.addArgumentValue(UpdateCommandStep.CONTEXTS_ARG, "dev");
            commandScope.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, "/database/changelog.sql");
            commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.DATABASE_ARG, database);
            commandScope.addArgumentValue(DatabaseChangelogCommandStep.CHANGELOG_PARAMETERS, changelogParameters);**
            commandScope.execute();
        });

```

---

<div class="post-metadata">

**Author:** ![balistico](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@balistico](https://forum.liquibase.org/u/balistico)\
**Post date:** [January 19, 2024, 1:40pm UTC](https://forum.liquibase.org/t/pass-a-variable-to-an-sql-changelog-at-runtime/9013/5 "2024-01-19T13:40:00Z")

</div>

@wwillard Amazing! That was exactly what I was looking for! 🙂 Thank you!
