Join 2 tables from different DB - sql

How can I join 2 tables from different DB with SQL Server?

Simply just use multipart identifier names if they are in the same server.
SELECT *
FROM [database_1].[dbo].[table_1] t1
JOIN [database_2].[dbo].[table_2] t2
ON t1.id = t2.id
If not you can use linked servers and 4-part names.
SELECT *
FROM [database_1].[dbo].[table_1] t1
JOIN [linked_server].[database_2].[dbo].[table_2] t2
ON t1.id = t2.id

Related

Joining Table via Linked Server

I have 2 servers with 2 different databases.
S1(Server) WITH DB1(Database) WITH T1(Table)
and
S2(Server) WITH DB2(Database) WITH T2(Table)
DB1 has a linked server to S2 DB2.
So i can run a query such as
SELECT * FROM T1
JOIN [S2].[DB2].[T2] T2
ON T1.[ID] = T2.[ID]
And this runs correctly.
However, how can i run it in the opposite direction
e.g.
SELECT * FROM T2
JOIN [S1].[DB1].[T1] T1
ON T2.[ID] = T1.[ID]
This errors with "Could not find server [S1].[DB1].[T1]"
Is there anyway i can join in this direction without having to create a linked server from S2 to S1?
This query:
SELECT *
FROM T2 JOIN
[S1].[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
Presumes that T2 is local. You can sort of do a hybrid:
SELECT *
FROM [S2].[DB2].[T2] T2 JOIN
[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
It is possible to link a server to itself, so if you did that, you could run this identical query on both servers:
SELECT *
FROM [S2].[DB2].[T2] T2 JOIN
[S2].[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
If you are trying to implement "equivalent" queries on two servers, then synonyms could help. You might be interested in the answers to this question.
Gordon's answer is more thorough but you can rewrite your second query as:
SELECT *
FROM [S2].[DB2].[T2] T2
JOIN T1
ON T2.ID = T1.ID

HQL query with Left Join in NHibernate 2

How can I give conditions for left join in NHibernate 2.0 HQL query.
Eg in SQL.
select t1.* from table1 t1
left join table t2 on t2.id = t1.id and t2.column2 = t1.column2
I tried the below HQL query, but got an exception "unexpected token: with"
select t1 from Table1 t1
left join t1.Table2 t2 with t2.column 2 = t1.column2
There is no need to use ON statement on (LEFT or any other) JOIN. That condition will be injected by mapping. There is the HQL doc:
14.3. Associations and joins
So, this HQL will work:
SELECT t1 from Entity1 AS t1
LEFT JOIN t1.ReferencePropertyToEntity2 t2
and the generated sql will be like this:
SELECT t1 from Table1 AS t1
LEFT JOIN Table2 t2
ON t1.ReferenceColumnToTable2 = t2.Table2_ID
But in case, that we want to do more restrictions, we can extend ON clause with more conditions - but they will be all applied with AND
SELECT t1 from Entity1 AS t1
LEFT JOIN t1.ReferencePropertyToEntity2 t2
WITH t2.IsActive = 1
OR t1.IsDeleted = 0
will result in
SELECT t1 from Table1 AS t1
LEFT JOIN Table2 t2
ON t1.ReferenceColumnToTable2 = t2.Table2_ID
AND (
t2.IsActive = 1 OR t1.IsDeleted = 0
)
So, in case that we want to use WITH to totally replace ON generated by mapping, we have to go different way - with CROSS JOIN:
NHibernate HQL Inner Join (SQL Server,Visual C#)
How to join Two tables of two non relashinship defined columns using Nhibernate QueryOver

SQL issue: JOIN on empty table

For example we have the following SQL query
select T1.name
from table1 T1
join table2 T2 ON T1.id = T2.id
I have made join table2 only if it is NOT empty. If table2 is empty I don't make join
select T1.name
from table1 T1
Can I solve it by one SQL query?
It would be good to use only SQL standard.

SQL - alternative to left outer join

There is a standard way in SQL to conut a number of rows joined to one table acepting also the 0?
That is one example :
SELECT t1.id, COUNT(t2.*)
FROM t1 LEFT OUTER JOIN t2 ON ( t1.id = t2.id )
GROUP BY t1.id
I need a alternative because i use odbc with different databases, and on some databases the left join aren't supported.
SELECT
t1.id,
(SELECT COUNT(*) FROM t2 WHERE t2.id = t1.id) as t2_count
FROM t1
Two Options:
Option 1: use the (+) operator:
SELECT t1.id, COUNT(t2.*)
FROM t1, t2
WHERE t2.id(+) = t1.id
GROUP BY t1.id
I don;t know if it works on all drivers. Option 2 that will work with all drivers is to create a view and create the view instead.

Join query where table references itself

I'm using Oracle 10, but the best way to ask this question is with an example.
select *
from t1, t2
where t1.id = t2.id
and t1.otherID = (select max(otherID)
from t2
where id = THE ID FROM THE OUTER QUERY T1
)
I think you see where I'm trying to go with this. I need to reference t1 in the subquery to join it to the max of t2.
I need to know how to create a query like this.
"THE ID FROM THE OUTER QUERY T1" is where my confusion is.
I tried using t1.id, but did not get results.
Try the following
select t1.*, t2.*
from t1
join t2 on t1.id = t2.id
join (select id, max(otherID) as max_otherID
from t2
group by id
) a ON a.id = t1.id and a.max_otherID = t1.otherID
Using a sub-query on the join often gives better performance than using it in the where clause.