SQL Query across multiple SQL servers - sql

I have 2 SQL servers. I need a SQL query that can join 2 tables that are in two different server.
Like
SELECT *
FROM Server1.Db1.dbo.table1 A
INNER JOIN Server2.Db1.dbo.table2 B ON A.Id = B.Id
and I do not have the server names, instead I am using IP address of the servers. Do I need to enable these SQL servers as linked server to allow such cross server queries?

You can proceed with Linked Servers using sp_addlinkedserver.
Once done, you can query your data as you mentioned;
SELECT *
FROM [Db1].[dbo].table1 A
INNER JOIN [Server2].[Db1].[dbo].table2 B
ON A.Id = B.Id

Yes, add as linker server is one option. You also can join the remote table by use [ip address].dbname.dbo.table name s well.

Related

How to join 2 tables from 2 different servers in AS400?

I am working with AS400 version 7.1.
Having the following:
ServerA (SA) - DatabaseA (DBA) - TableA (TA)
ServerB (SB) - DataBaseB (DBB) - TableB (TB)
SELECT A.*, B.*
FROM SA.DBA.TA A INNER JOIN SB.DBB.TB
ON A.PN=B.PN
WHERE A.PN='BFDKS';
What's the correct syntax to join 2 tables from two different servers in AS400?
I am getting the following error
Relational database SA not in relational database directory
I'm pretty sure this is not currently possible with Db2 for i...
3-part names are new to the i, and as far as I know are limited to
insert into mylib.mytable
(select * from remotedb.somelib.sometable);
see CREATE TABLE with remote subselect
Or in a trigger program..
see 3-part names in triggers
Db2 for LUW has such federation capabilities...
One work around I've seen is the use of a user defined table function (UDTF) to return rows from a remote Db2 for i database..
Not possible on DB2 for i. But like says Charles, you can do it :
-- run this instructions on server B
create table qtemp.SADBATA as (
SELECT A.* FROM SA.DBA.TA A
where A.PN='BFDKS'
) with data;
SELECT * FROM qtemp.SADBATA A INNER JOIN SB.DBB.TB ON A.PN=B.PN;

Joining tables from 2 different servers through linked server - SQL Server

I am finding it difficult to join 2 tables across 2 servers in SQL server.
I know i am able to do this through a linked server. On my server 1 it has server 2 in its linked server folder in SSMS.
Server 1 = S1
Server 2 = S2
How do i reference the tables for a linked server join. I have tried
SELECT *
FROM [S1].[db1].[dbo].[Order] T1
JOIN [S2].[db1].dbo.[Invoice] T2
ON T1.[OrderID] = T2.[InvoiceID]
All the table names and server names are correct and server 2 is in the linked server folder under 'S2' but when i run this it says S2 and its table are invalid object names.
Any help would be appreciated
You dont need to specify the local server on your query cause it's not a linked server. So change your query to:
SELECT *
FROM [db1].[dbo].[Order] T1
JOIN [S2].[db1].dbo.[Invoice] T2
ON T1.[OrderID] = T2.[InvoiceID]

Incoming linked servers information in SQL Server

Is there anyway that we can list out the linked servers coming to a SQL server? I am trying to catch all the connections coming to a SQL Server. Please help!!
Thanks
Execute prcoedure: sp_linkedservers
MSFT article describing this procedure
or maybe if you want more info:
SELECT *
FROM sys.Servers a
LEFT OUTER JOIN sys.linked_logins b ON b.server_id = a.server_id
LEFT OUTER JOIN sys.server_principals c ON c.principal_id = b.local_principal_id

sql query for join two tables of different databases that are in two Servers

i have two tables tableA in databaseA on ServerA and tableB in databaseB on ServerB.
i just want to perform fullouter join to these tables based on common fieldname of it
In SQL Server, you can create a linked server (in Management Studio, that's under Server Objects.) Then you can use a four part name to join the tables:
select *
from localdb.dbo.localtable as t1
full outer join
linkedserver.remotedb.dbo.remotetable as t2
on t1.col1 = t2.col1
If you're using another database, please edit your question to say which.

Join two tables at the same server

I have two databases at the same server 192.168.1.100 DB1 and DB2
When I'm trying to execute :
select h.code,eh.Defaultname From hotels h JOIN [192.168.1.100].[dbo].[DB2].Hotels eh ON h.code = eh.code
I get
Could not find server '192.168.1.100' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.
I don't understand :/
I'm using ms sql server 2005 and this query is fired when I'm at DB1.
What's the reason of this , and how to fix it ? :/
Since the databases are on the same SQL Server instance, you don't need to use a linked server, so don't specify the IP, just the other database's name.
select h.code,eh.Defaultname
From hotels h
JOIN [DB2].dbo.Hotels eh ON h.code = eh.code
JOIN [DB2].[dbo].[Hotels] AS eh