Question : T-SQL how to compare two tables from two different database servers

I need some help on creating a query that will enable me to compare to tables from two different sources.  The following are my sources:

SharePoint Server (WSS_Content)
ERP Server (ESIDB)

So far what I have done is create a data link into our erp server from our sharepoint database server.

I have so far a simple query that queries both servers for the following

PO Number
VendorName
Amount

Our sharepoint server has metadata fields that are automatically filled in when a user clicks the autofill button in a sharepoint editform

What i need to do is guarantee that the data in both systems are always the same by producing a weekly report that compares both data sources

Below is my code for query both data sources.

Please Note:  that the erp server's table may contain far more POs than the sharepoint server,  but the PO numbers will always be unique.  So the PO number will be my unique identifier

thank you in advance for all of your help
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
use WSS_Content
-- select from the sharepoint server
select a.nvarchar10 as [PO Number], a.nvarchar11 as [Vendor Name],
convert(decimal(18, 2), a.float1) as [Amount] from AllUserData as a
  inner join (SELECT tp_id, max(tp_Modified) AS max_date
                FROM AllUserData where tp_dirName = 'demsite/Test Documents'
                GROUP BY tp_id) as b
 on b.tp_id = a.tp_id 
 where a.tp_dirName = 'demsite/Test Documents' and b.max_date = a.tp_Modified
 order by a.tp_LeafName
 
--erp server.  the erp server can older POs in it and there
--can be more than the SharePpoint Serve, but the PO number
--will always be unique so they will match on each server
select po_id,  Vendor_Name, revised_po_amt
from [ERPSRV].[ESIDB].[dbo].[POFOM]
where po_id = 'F27536' or po_id = 'F27536G' or po_id = 'F64499' or po_id = 'F27689' or po_id = 'F27699'
or po_id = 'F27747' or po_id = 'F27751' or po_id = 'F27801' or po_id = 'F27784' or po_id = 'F27698'

Answer : T-SQL how to compare two tables from two different database servers

Ooops, code snippet got lost!?


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
use WSS_Content
select a.nvarchar10 as [PO Number],
       a.nvarchar11 as [Vendor Name],
       convert(decimal(18, 2),a.float1) as [Amount],
       erp.Vendor_Name,
       erp.revised_po_amt
  from AllUserData as a
  inner join (SELECT tp_id, max(tp_Modified) AS max_date
                FROM AllUserData
               where tp_dirName = 'demsite/Test Documents'
               GROUP BY tp_id) as b
     on b.tp_id = a.tp_id
  inner join [ERPSRV].[ESIDB].[dbo].[POFOM] erp
     on erp.po_id = a.nvarchar10
 where a.tp_dirName = 'demsite/Test Documents'
   and b.max_date = a.tp_Modified
 order by a.tp_LeafName
Random Solutions  
 
programming4us programming4us