Q: Inserting data from two tables which may have matching columns I have 3 Tables: Tbl1, Tbl2 and Tbl3. 
Tbl3 has a structure such that most Columns in Tbl3 are present in Tbl2 + Tbl1. 
I don't know which columns match and which ones don't.
I want to insert data from Tbl1 and Tbl2 to Tbl3.
Is there a way to determine Column names that match Tbl3 and then insert data from Tbl1 and Tbl2?
Possible structure of tables is like:

Tbl1 ---> Column1 | Column2 | Column3 |.... Column78
Tbl2 ---> Column79 | Column80 | Column81 |.... Column 100
Tbl3 ---> Column1 | Column2 | Column3 |.... Column100 | Column101 |
  Column102... Column 120


A: You can get the column names from INFORMATION_SCHEMA.columns.  You then have to manually create the query yourself:
select tbl3.table_name,
       (case when ct1.TABLE_NAME is not null then 'Y' else 'N' end) as InTbl1,
       (case when ct2.TABLE_NAME is not null then 'Y' else 'N' end) as InTbl2
from INFORMATION_SCHEMA.columns ct3 left outer join
     INFORMATION_SCHEMA.columns ct1
     on ct1.COLUMN_NAME = ct3.COLUMN_NAME and
        ct1.TABLE_NAME = 'tbl1' left outer join
     INFORMATION_SCHEMA.columns ct2
     on ct2.COLUMN_NAME = ct3.COLUMN_NAME and
        ct2.TABLE_NAME = 'tbl2' left outer join
where ct3.TABLE_NAME = 'tbl3';

With this information you can then construct the query that you want.
