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

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]

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;

How to select data from server and insert it into another server in informix DB

If i have two tables with the same structure Table1,Table2 in different databases DB1,DB2 in two different servers S1 And S2.
How to Select from Table1 and Insert Into Table2 with a simple query ?
I try to execute the following but in vain:
INSERT INTO DB2:Table2
SELECT * FROM DB1#S1:Table1
I get the following error :
If you are connecting to S1 (informix Server 1) the SQL will be executed in that server, which means S1 will need to know where S2 is.
Basically make sure that there is a S2 defined in the sqlhosts file used in S1 (and that you can connect to S2 using the same user you are connecting to S1)
It's not enough to have S1 and S2 defined in the client 'sqlhosts' (server studio) as it is the Informix server (not the client) who will do the connection to S2 for the insert

SQL Query across multiple SQL servers

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.

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