How can I select data in the same query from two different servers and databases from SQL Server Management Studio? - sql

How can I select data in the same query from two different databases that are on two different servers, one DB2 Server and the other a SQL Server?

On your sql server, set up a linked server to the db2 database.
Then write your query on sql server. I suggest that you use openquery for the db2 stuff. If you have to combine the data, populate a sql server temp table with the openquery results and work from there.
The reason I suggest this is performance. I have found that if you use this syntax
select somefields
from server.database.owner.table
where whatever
sql server will bring back the entire table from the linked server and apply the where clause afterwards.

You can set up a linked server http://support.microsoft.com/kb/222937
How to create a linked server

Related

Query tables between two different servers in SQL Server

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)

How to use two servers in one sql statement?

So I need to try and use two different servers in one SQL statement. Here is my query so far:
SELECT p.FirstField
from [FirstDatabase]..FirstTableName p
Where not exists (select p2.SecondField
from [SecondServer].[SecondDatabase]..SecondTableName p2
where p.FirstField = p2.SecondField)
I do not use the First Server in the first part of the statement because that Server has already been selected.
I am using Microsoft SQL Server
You need to set up the remote server as a linked server using sp_addlinkedserver. Then you can refer to the remote table using the linked_server_name.catalog.schema.object_name syntax. You can find more information here.

SQL statement joining Oracle and MS SQL Server

I never seen this, but is it possible to have one SQL call join data from Oracle and SQl Server?
Yes, Oracle and SQL Server both have functionality that allows to connect to other databases, including different vendors. In Oracle terminology, it's a database link instance while on SQL Server it's called a Linked Server instance.
The syntax to reference the instance is different between Oracle and SQL Server though. IE:
Oracle:
SELECT t.*
FROM table_name#database_link_instance t
SQL Server:
SELECT t.*
FROM linked_server_instance_name.database_name.schema_name.table_name t
does MySQL support the linked server concept?
No, the closest MySQL has is the FEDERATED engine, which is only for connecting to remote MySQL instances.
PostgreSQL?
PostgreSQL has dblink. Last time I looked at dblink (pre-v9 release), it only could connect to other PostgreSQL instances.
Yes- both Oracle and SQL Server support the linked server concept. That allows you to reference the other server using a 4 part name. For example:
select *
from LocalDb.Schema.Table
cross join
OracleLinkedServer.RemoteDb.RemoteSchema.RemoteTable

SQL Server 2005 query multiple Access databases?

Is there a way to get SQL Server 2005 to query 3 or more separate Access databases (each returning 1 record) in one SQL statement?
Yes, but it will require you to create a Linked Server instance for each Access database. See here for details about creating Linked Server instances on SQL Server 2005 to MS Access.
Once you have those in place, you can query SQL Server and it will pass on the queries to the respective Access databases based on using the Linked Server instance notation when specifying tables in your SQL Server queries.
What you want is a Linked Server for each of the Access databases.
Just be aware that in SQL 2005 64-bit you won't be querying current versions of Access or Excel through linked servers any time soon.
Yes, set them up as linked servers using sp_addlinkedserver.

Oracle and SQL Dataset

Problem: I have to pull data from a SQL Server database and an Oracle database and put it together in one dataset. The problem I have is: The SQL Server query requires an ID that is only found from the return of the Oracle query.
What I am wondering is: How can I approach this problem in a way such that performance is not harmed?
You can do this with linked servers or by transferring the data all to one side. It's all going to depend on the volume of data on each side.
A general rule of thumb is to execute the query on the side which has the most data.
For instance, if the set of Oracle IDs is small, but the SQL Server set is large, you make a linked server to the Oracle side and execute this on the SQL Server side:
SELECT *
FROM sqlservertable
INNER JOIN linkedserver.oracletable
ON whatever
In this case, if the Oracle side is large (or cannot be prefiltered before the need to join with the SQL Server side), the performance will normally be pretty poor - and will improve a lot by pulling the entire table (or the minimal subset you can determine) into a SQL Server table instead and do the JOIN all on the SQL Server side.