Search code examples
oracle-databaseunit-testingplsqlutplsql

utPLSQL: How can i compare two tables?


Lets say i have table 1 and table 2. Table 2 is the updated version of table one. It can have same or updated structure, columns and data. I want to compare those two tables.


Solution

  • My requirement was to compare columns, data and constraints of two tables using utPLSQL. i met my requirement by using native refcursors.

    For data comparison:

    OPEN p_store FOR SELECT * FROM customers@dblink2;
    OPEN p_store2 FOR SELECT * FROM customers2@dblink2;
    ut.expect(p_store).to_equal(p_store2);
    

    For column comparison:

    OPEN p_store FOR
    SELECT
        A.COLUMN_NAME,
        A.DATA_TYPE,
        A.DATA_LENGTH
    FROM
        (SELECT * FROM USER_TAB_COLUMNS@dblink2 WHERE TABLE_NAME  = 'CUSTOMERS') A;
    OPEN p_store2 FOR
    SELECT
      B.COLUMN_NAME,
      B.DATA_TYPE,
      B.DATA_LENGTH
    FROM
      (SELECT * FROM user_tab_columns@dblink2 WHERE table_name = 'CUSTOMERS') B;
    ut.expect(p_store).to_equal(p_store2);
    

    I using utPLSQL V3. If you're using v2 then utassert.eqtable can be used.