Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Table1 in Database1 in Server1 and i want to join a column1 in table1 in database1 in server2 to the Table1 in Database1 in Server1.

What I have tried:

I ran a script as follows to connect the 2 servers

SQL
exec sp_addlinkedserver 'server2'


and then i used this script to join my 2 tables

SQL
SELECT Table1.Column1, [server2]. 
       [database1].dbo.table1.column1
FROM Table1, [server2].[database1].dbo.table1.column1
LEFT OUTER JOIN [server2].[database1].dbo.table1 ON(Table1.Column1 = [server2]. 
                [database1].dbo.table1.column1)
ORDER BY table1.column1
Posted
Comments
Santosh kumar Pithani 25-Apr-18 6:24am    
If you created linked server then use "select t1.col,t2.col2 from [linkedserver1].[db].dbo.table1 AS t1 INNER JOIN [linkedserver2].[db].dbo.table2 AS t2 ON(t1.col1=t2.col1)"
Member 13475897 25-Apr-18 6:39am    
Thank you, this sort of worked. The only problem is my table has no data in.Please find my full query below and ask me to write it over in terms of table1, etc. if you need that.

select OEINVH.INVNUMBER,OEINVH.ORDNUMBER,OEINVH.SHINUMBER,OEINVH.CUSTOMER,
OEINVD.LINENUM, ICITEM.ITEMNO,ICIOTH.MANITEMNO[Barcode], OEINVD.QTYSHIPPED,OEINVD.INVUNIT,OEINVD.UNITPRICE,
OEINVD.EXTINVMISC[Line Total], SalesOrderDetail.sord_Name
from [SVWSUSAV].[LOGDAT].dbo.OEINVH AS OEINVH
LEFT OUTER JOIN OEINVD ON(OEINVH.INVUNIQ = OEINVD.INVUNIQ)
LEFT OUTER JOIN ICITEM ON(OEINVD.ITEM = ICITEM.FMTITEMNO)
LEFT OUTER JOIN ICIOTH ON(ICITEM.ITEMNO = ICIOTH.ITEMNO AND ICIOTH.UNIT = OEINVD.INVUNIT)

INNER JOIN [CRM\SQLEXPRESS].[CRM_Test].dbo.SalesOrderDetail AS SalesOrderDetail
ON(OEINVH.CUSTOMER = SalesOrderDetail.sord_Name COLLATE DATABASE_DEFAULT)

WHERE OEINVH.CUSTOMER IN('SHO006','SHO005')
AND OEINVH.INVUNIQ NOT IN(SELECT INVUNIQ FROM InvoiceSent)
AND OEINVH.INVDATE >= 20180415
order by OEINVH.INVNUMBER,OEINVD.LINENUM
Santosh kumar Pithani 25-Apr-18 7:02am    
you have used table names multiple times without giving proper aliases,ambiguity issues will occur.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900