简体   繁体   中英

How to copy the the data from one Table to another Table Using Liquibase scripts

I want to know the solution for the below Test ( to copy the the data from one Table to another Table) using Liquibase a part of Corda.

Case: As part of I want to create a new table into an existing database which has already a table called TableA (which has id, name, value columns) which has some data init, I have created a TableB (which has same id, name, value columns) and I wanted to copy the data from TableA to TableB.

For that I had used the following liquibase script as suggested here in Liquibase and In-order to test I had connected to PostgreSQL DB and connected schema called "corda_schema" which has the tables.

<changeSet author="liquibase.corda" id="update-table">
      <update schemaName="corda_schema" tableName="TableB">
        <column name="id" valueComputed="(SELECT id from TableA)"/>
    <column name="name" valueComputed="(SELECT name from TableA)"/>
    <column name="value" valueComputed="(SELECT value from TableA)"/>
      </update>
</changeSet>

I was getting the following error when I tried with the Liquibase update script

Error: liquibase.corda failed
Error: schema "CORDA_SCHEMA" not found in SQL statement

If I don't given the schema name in update like this

<update tableName="TableB">
    <column name="value" valueComputed="(SELECT value from TableA)"/>
</update>

the Liquibase is searching in the Public schema for TableA and I get this error:

Error: liquibase.corda failed Error: schema "PUBLIC" not found in SQL statement`

And also I tried this Liquibase script changeSet by creating the table itself I tried to update data, this changeSet is running and table is created but data is not copied.

<changeSet author="liquibase.corda" id="update-table">
  <createTable schemaName = "corda_schema" tableName="TableB">
    <column name="id" valueComputed= "(SELECT id FROM TableA)"/>
</createTable>
</changeSet>

Please suggest anything I am missing or any other usages that will make my test success to get the data from one table to another table.

Thanks in advance.

I would suggest to just use custom sql:

<changeSet author="liquibase.corda" id="insert-table">
      <sql>
        insert into corda_schema.TableB
        select id,name,value from corda_schema.TableA;
      </sql>
</changeSet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM