How to join 2 tables from different databasess - sql-server

Is there any way to join 2 tables from different databases in a query? The first table comes from an MS Access database, and second one come from a SQL Server database

you can use OpenRowSet() in sql server management studio to access your accessdb database tables
SELECT * From OpenRowset('Microsoft.Jet.OLEDB.4.0',
';Database=C:\yourSampleDatabase.mdb;',
'SELECT * from YourTableName') as linkedQueryToAccess
and on this result set you can do the join operation with your table present in mssql database

You could make a linked server in SQL Server between Access and SQL Server and easily query it. But it would be very slow.

Related

SSIS, query Oracle table using ID's from SQL Server?

Here's the basic idea of what I want to do in SSIS:
I have a large query against a production Oracle database, and I need the following where clause that brings in a long list of ids from SQL Server. From there, the results are sent elsewhere.
select ...
from Oracle_table(s) --multi-join
where id in ([select distinct id from SQL_SERVER_table])
Alternatively, I could write the query this way:
select ...
from Oracle_table(s) --multi-join
...
join SQL_SERVER_table sst on sst.ID = Oracle_table.ID
Here are my limitations:
The Oracle query is large and cannot be run without the where id in (... clause
This means I cannot run the Oracle query, then join it against the ids in another step. I tried this, and the DBA's killed the temp table after it became 3 TB in size.
I have 160k id's
This means it is not practical to iterate through the id's one by one. In the past, I have run against ~1000 IDs, using a comma-separated list. It runs relatively fast - a few minutes.
The main query is in Oracle, but the ids are in SQL Server
I do not have the ability to write to Oracle
I've found many questions like this.
None of the answers I have found have a solution to my limitations.
Similar question:
Query a database based on result of query from another database
To prevent loading all rows from the Oracle table. The only way is to apply the filter in the Oracle database engine. I don't think this can be achieved using SSIS since you have more than 160000 ids in the SQL Server table, which cannot be efficiently loaded and passed to the Oracle SQL command:
Using Lookups and Merge Join will require loading all data from the Oracle database
Retrieving data from SQL Server, building a comma-separated string, and passing it to the Oracle SQL command cannot be done with too many IDs (160K).
The same issue using a Script Task.
Creating a Linked Server in SQL Server and Joining both tables will load all data from the Oracle database.
To solve your problem, you should search for a way to create a link to the SQL Server database from the Oracle engine.
Oracle Heterogenous Services
I don't have much experience in Oracle databases. Still, after a small research, I found something in Oracle equivalent to "Linked Servers" in SQL Server called "heterogeneous connectivity".
The query syntax should look like this:
select *
from Oracle_table
where id in (select distinct id from SQL_SERVER_table#sqlserverdsn)
You can refer to the following step-by-step guides to read more on how to connect to SQL Server tables from Oracle:
What is Oracle equivalent for Linked Server and can you join with SQL Server?
Making a Connection from Oracle to SQL Server - 1
Making a Connection from Oracle to SQL Server - 2
Heterogeneous Database connections - Oracle to SQL Server
Importing Data from SQL Server to a staging table in Oracle
Another approach is to use a Data Flow Task that imports IDs from SQL Server to a staging table in Oracle. Then use the staging table in your Oracle query. It would be better to create an index on the staging table. (If you do not have permission to write to the Oracle database, try to get permission to a separate staging database.)
Example of exporting data from SQL Server to Oracle:
Export SQL Server Data to Oracle using SSIS
Minimizing the data load from the Oracle table
If none of the solutions above solves your issue. You can try minimizing the data loaded from the Oracle database as much as possible.
As an example, you can try to get the Minimum and Maximum IDs from the SQL Server table, store both values within two variables. Then, you can use both variables in the SQL Command that loads the data from the Oracle table, like the following:
SELECT * FROM Oracle_Table WHERE ID > #MinID and ID < #MaxID
This will remove a bunch of useless data in your operation. In case your ID column is a string, you can use other measures to filter data, such as the string length, the first character.

Using a SQL Server openquery to query a linked DB2 server table using conditions based on a SQL Server table

I have a result set I need to pull in from a linked DB2 server table into SQL Server. The table is huge, and I don't want or need to pull the whole thing, I only need the records for a handful of users. The problem is the User IDs are stored in a SQL Server table, not on the DB2 table. While I have select privileges on the DB2 server, I cannot create a table there, so as far as I'm aware I cannot upload the table with User IDs onto the DB2 server. Is there a way to limit the result set pulled from the DB2 server on the User IDs stored in the SQL Server table?

Union view (query) between SQL Server and Oracle tables

I have 2 tables, one is in SQL Server, the other is in Oracle (this is driven by the software that feeds the databases and cannot be changed).
Currently I am using MS Access and have pulled both of these tables in as 'linked tables'. MS Access makes it very easy to pull in data from a variety of different data sources (Oracle, SQL Server, XML, Excel etc.) and then query as if the tables were in the same environment.
I want to have the same view in SQL Server. Is this possible?
The reason I want this is because as it stands, for anyone to use my MS Access query, they need the ODBC connections on their local workstations. Many of the workstations do not have the Oracle driver installed.
I want to push the ODBC requirement to a server and then just use a SQL Server view in the MS Access database and remove the linked tables. Every workstation has a SQL driver which would make this much easier for me.
**SQL Server** **ORACLE**
TABLE_A TABLE_B
Name Name
Description Description
VALUE1 VALUE1
STATUS STATUS
Union query in MS Access is:
SELECT DISTINCT *
FROM TABLE_A
UNION
SELECT DISTINCT *
FROM TABLE_B;
I want a view in SQL Server that replicates this Union query.
You need to create a "linked server" on SQL Serwer to Oracle database.
https://www.mssqltips.com/sqlservertip/4396/creating-a-sql-server-2014-linked-server-for-an-oracle-11g-database/
Then you will use a query like this:
SELECT DISTINCT *
FROM TABLE_A
UNION
SELECT DISTINCT *
FROM Oracle_link_server..schema.table_b;

How to copy a table from database A on a linked server to database B?

I am using SQL Server 2012 and I need to write a T-SQL query (that I will use as a SQL job) to copy a table (say, T1) located in a database (say, db1) on a linked Server to another database (say, db2).
Table T1 does not currently exist in database db2.
Assuming the table schema of T1 on the linked server is [xxx.xx.x.xx].db2.dbo.T1, how do I write this T-SQL query?
You may use OPENDATASOURCE. In Your destination Server database, Just Run
SELECT
*
INTO dbo.DestinationTable
FROM
OPENDATASOURCE
(
'SQLOLEDB',
'Data Source=SourceServer;User ID=MyUser;Password=MyPass'
).SourceDatabase.dbo.SourceTable;

How do I access multiple SQL Server databases in an instance from Oracle?

I've created an ODBC connection, and it works fine. My issue is about accessing more than one database on the same SQL Server instance from Oracle. I had ideally not wanted to create multiple DSNs/config changes.
Is this possible to do from Oracle? No is fine, I just want to know.
SELECT a.col1, b.col2
FROM BobDole.dbo.BobDole#linkedserver AS a
JOIN BobDole2.dbo.BobDole#linkedserver AS b
WHERE b.col3 = 123;
Or even better, if it exists, an OPENQUERY() allowing dynamic SQL from Oracle->SQL Server so I don't have to join across the linked server.

Resources