Dynamically comparing two tables from two different databases and serves in SQL Server Management Studio

811 Views Asked by At

I am working on a project, where a user will select a a table choice on a website. Once the table is selected, the website will then connect to the database and select the table from a server (Server A) under a database (ABC). The website will also have to choose the same table from a different server (Server B) under database (DEF). Also these tables will have the same name, they will have some different data entered into them.

Our goal is to come up with a dynamic SQL Query/Stored Procedure. Multiple table choices can be visible in the website and once the user picks an option, it DYNAMICALLY passes that information to the database to find the two tables and yield a final table that portrays the differences.

MY PROBLEM:

  1. I am having a lot of problem with the syntax and doing this process dynamically. I have searched everywhere for a solution and am struggling with this for more than two weeks.

OUTLINE OF MY PLAN:

  1. Find primary keys and the column names of all the columns in a selected table. Pass this information to a temporary table

  2. Create a SQL query with something like :

SET @SQL = select table1.col1, table2.col1... inner join..

  1. Take care of conditions where :

    A. Data is present in one table but not the other

    B. Data is present in both tables

    C. What if there is no data in one or both tables.

I would really appreciate any help. I am very new to SQL and have been trying my hardest in this project for a while. Please help me and I will do my best to repay you. Thank you very much for your time.

0

There are 0 best solutions below