I need to create a dynamic procdedure where I must compare the columns of two tables. The end result SQL looks like this
SELECT "ID", "FIELD_1", "FIELD_2", "FIELD_3","FIELD_4","FIELD_5"
FROM (
SELECT "ID", "FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5"
FROM TABLE1
MINUS
SELECT "ID", "FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5"
FROM TABLE2 )
UNION ALL
SELECT "ID", "FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5"
FROM (
SELECT "ID", "FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5"
FROM TABLE2
MINUS
SELECT "ID", "FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5"
FROM TABLE1 );
I'm using this query to get the column from all tables:
SELECT * FROM TABLE_COLUMNS
WHERE SCHEMA_NAME ='MY_SCHEMA' AND TABLE_NAME='TABLE1' ORDER BY POSITION;
In this code the number of fields in each table is six, but this will vary as per tables.
If I were to do this in any other language, I'll write a function which takes the table names in argument get the column names and save them in an array and iterate through them and concatenate to the query. I'm new to SQL so I don't know if its even possible to do this. Is there a way to implement this?