Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join tables from different databases, servers in SQL and C#

Tags:

c#

sql

I have one table called "EEmailSentdata" which belongs to database "A" and another table "EEventGuest" which belongs to database "B", I want to make a query by joining these tables, is it possible?

like image 837
Shilpa Soni Avatar asked Feb 14 '26 15:02

Shilpa Soni


2 Answers

If both database are on the same SQL Server it is pretty simple. You just prefix the table names with the database name and the name of the schema. If they are not on the same SQL instance you will have to create a connected server object and prefix the table with that objects name. Thats it

Create a linked server:

http://msdn.microsoft.com/en-us//library/ff772782.aspx

like image 72
Romano Zumbé Avatar answered Feb 16 '26 07:02

Romano Zumbé


If they are on same server, and depends which JOIN you need:

SELECT * FROM A.EEmailSentdata a
(CROSS/LEFT/INNER) JOIN B.EEventGuest b ON a.ID = b.ID
like image 27
mirkobrankovic Avatar answered Feb 16 '26 06:02

mirkobrankovic