scriptella

Handling empty resultset from a query


I have two databases with the same structure and I want to compare records between databases. The records in second database are copied from the first database, but the copying process sometime doesn't work and in the first database in one table I have more records than in the same table in the second database. So I want to know which records from the first database doesn't exists in the second database. I have tried with something like that:

<etl>
<connection id="db1" driver="auto"
    url="jdbc:mysql://localhost:3306/db" user="user"
    password="xxx"
    classpath="C:/mysql-connector-java-5.1.20.jar" />
<connection id="db2" driver="auto"
    url="jdbc:mysql://localhost:3307/db" user="user"
    password="xxx"
    classpath="C:/mysql-connector-java-5.1.20.jar" />
<connection id="text" driver="text" />
<query connection-id="db1">
    SELECT * FROM table;
    <query connection-id="db2">
        SELECT * FROM table WHERE id = '$id';
        <script connection-id="text">
            sometext, $rownum
        </script>
    </query>
</query>
</etl>

The problem is, when the result of the query against db2 is empty the script is not executed.

How to solve this problem?

Regards, Jacek


Solution

  • You can use count to check the actual number of records. In this case resultset will always return one row. Example:

    <query connection-id="db1">
        SELECT * FROM table;
        <query connection-id="db2">
            SELECT count(id) as CNT FROM table WHERE id = ?id;
            <!-- The script is executed ONLY IF number of results is zero -->
            <script connection-id="text" if="CNT==0">
                No matching record for id $id
            </script>
        </query>
    </query>