This question already has answers here:
Querying data by joining two tables in two database on different servers
(11 answers)
Closed 4 years ago.
How can I fetch the data from two different databases that are on different servers in SQL Server?
You will need to create a linked server to the server where you will execute the Query.
You can use sp_addlinkedserver to create one. You may reference here.
Sample would be for the same SQL Server
EXEC sp_addlinkedserver
N'SEATTLESales',
N'SQL Server';
GO
Once you have created the linked server. You can now reference it in your query by doing something like this:
SELECT * FROM [NameOfLinkedServer].[DatabaseInLinkedServer].[dbo].[TableInLinkedServer]
Or you can check Dave Brown's comment to your question: Here.
You can indeed do this, but I recommend against it. You can create a linked server on DB-SERVER-1, connecting it to DB-SERVER-2.
(A linked server definition is setup by the DBA. It contains the login credentials to the remote server. If you want to provide the login credentials yourself each time, that would be an openquery or openrowset command.)
You then could write a query using four part naming convention as follows, assuming you are running this query on DB-SERVER-1:
select *
from DB1.dbo.YourTable a
join [DB-SERVER-2].DB2.dbo.OtherTable b on b.ID = a.ID
Cross server joins are notorious for having performance problems. I would only do it for administrative purposes, and definitely not against large result sets.
Related
I have two databases, one in SQL Server and one in Azure SQL Synapse. Now I want to have one single query that should be done between two tables from these different servers.
For example:
SQL Server name : server1
Database name : db1
Table name : tb1
Azure Synapse name : prod-synapse-2
Database name : db2
Table name : tb2
Now query should be like this:
select
tb1.col1, tb1.col2, tb2.col3, tb2.col4
from
tb1
outer join
tb2 on tb1.col5 = tb2.col5
The above query is very simple to join two tables from the same database or two tables from the 2 different database within same server.
But I want suggestions to have the above query between 2 different servers.
I have seen in many stackoverflow questions similar to this, they suggested an answer using Linked Server, but that cannot be done in my case, because I will not be provided with access to create Linked server, and link the 2 servers in Microsoft SQL Server Management Studio.
Thanks in advance
Another way to query a database hosted in a remote SQL Server is the OPENROWSET T-SQL function. To use the OPENROWSET ad hoc method, you need to provide all connection information that is required to connect to the remote SQL server and many other resources.
Using OPENROWSET requires enabling the Ad Hoc Distributed Queries advanced configuration option same as the OPENDATASOURCE function.
You need to provide the provider’s name, the connection string and the query as follows:
OPENROWSET(‘providername’,’datascource,’query)
I have an sql linked server whose data I can access using openquery, but I have no idea of how to see the tables of that database, hence i cant do much.
I only have a query
SELECT * FROM OPENQUERY(MYSERVER, 'SELECT * FROM SERVXML.DATA AS A WHERE A.DATAID = 2355')
Which returns some stuff.
But otherwise how can I see all the info in MYSERVER? I mean all the tables more specifically.
I have tried using this
EXEC sp_tables_ex 'MYSERVER';
With no result.
I'm a little confused. If you have a linked server, then you can just access the tables using a four-part naming convention: ...table.
This is explained in the documentation.
You can see the linked servers using metadata tables and views. For instance:
select *
from <server>.<database>.INFORMATION_SCHEMA.TABLES
I have four SQL Servers that are named in the following way:
dbs
dbs2
dbs3
dbs4
I have a table that is on dbs3 called table1 in database1. This table does not exist on the other servers. However when I run the query:
select *
from dbs.database1.dbo.table1 (or any of the database servers)
it returns the results as if I queried the existing table on dbs3. It is like the DBMS is ignoring the 4 part nameing in the query and returning the results from table on dbs3 no matter which server I try to designate in the 4 part naming convention. Any ideas what could be going on here. The servers appear in the linked servers list.
If you can make changes without breaking stuff (or if it's already broken enough in your opinion), I recommend recreating your linked servers. If your linked server is another SQL Server, you can do
exec sp_dropserver 'dbs';
exec sp_addlinkedserver 'dbs';
This creates a linked server definition with the default configuration, which is appropriate for most applications (and can still be tweaked afterwards).
I am trying to find a way of linking data from two servers in SSRS report builder 2.0
ultimately using one dataset to query 2 databases in 2 different servers.
The SQL that i have is very basic as shown below and i can run the query easily in SQL management studio.
select * from
[server1].[DES].[dbo].[Daily] dr
LEFT JOIN [server2].[VES].[dbo].[Rou] mr
ON dr.ID = mr.id
Also the access i have to the reporting server is read only so i cant really make any administrative changes to the configuration.
I have explored the connection string to create a connection but it only allows me to connect to one server for each dataset. what i am trying to do is use one dataset
Any help will be greatly appreciated as i am a junior in SQL and SSRS
Using SSRS and MS SQL 2008 R2
if you executing this query from your SSMS it means this other server
is a linked server. then it should be no different to execute it from
SSRS.
Yes it is right you can only add one server to your data source of
your dataset whether its a shared data source or embedded.
But for instance if you have a data source pointing to say Server A
when you executing queries which will be pulling data from Server A
and also from server B you will Use fully Qualified name for the
Objects from server B and two part name from server A.
something like this ...
SELECT *
FROM Schema.Table_Name A INNER JOIN [ServerNameB].DataBase.Schema.TableName B
ON A.ColumnA = B.ColumnA
obviously ServerB has to be a Linked Server.
I can think of two possible solutions
Option 1:
Creating a stored procedure on server1 (With a query accessing the another server). And call this stored procedure from SSRS (Dataset).
Option 2:
Create two dataset with two different connection strings on the RDL (one for Server1 and another for Server2).
Use the "LookUpDataSet" function in Report Builder to merge the results using the key fields.
Option 2 is preferable.
I need to get data from 3 different databases on one event command. Can anybody tell me any efficient way besides I am querying to all three different database servers in a row:-
Server 1 : Select * from ....
Server 2 : Select * from.....
and so on...
Thanks very much
Seeing as the question is marked TSQL:
Install the providers for the 'other' databases.
In SQL Server 2005, create a linked server to each database, and then simply query as though the 'other' databases were SQL Server databases.
If the databases are on the same server instance, they can be queried in the format "database_name.table_name.column_name" otherwise I would use Mitch's answer (linked servers can be queried in the format "server_name.database_name.table_name.column_name")