George Mastrokostas
George Mastrokostas

Reputation: 133

Postgres: Compare all fields from two tables and report back differences

I have two tables that have the same fields. I want to compare each field of one table with the fields of the other table and report back any difference in the data.

I would like to do this in a dynamic way with out having to hard code the name of the fields in my query. Is this even possible or should I find a solution to this by using Python?

Example of tables:

     TABLE 1                    TABLE 2
FIELD1 : VALUE1            FIELD1 : VALUE1
FIELD2 : VALUE2            FIELD2 : VALUE2
FIELD3 : VALUE3            FIELD3 : VALUE3xxx

Query goes through all the fields and then says "A ha" Field3 do not match, I must show this to the end user"

Upvotes: -1

Views: 1146

Answers (1)

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48187

You have to create a Dinamic Sql and put it inside a function

Dynamic sql in postgres

For start you need contruct a sql to compare field name

And to get a table field names

select column_name from information_schema.columns where
table_name='table 1';

Upvotes: 0

Related Questions