Union view (query) between SQL Server and Oracle tables - sql-server

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;

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.

sql server join query does not include table names for columns with the same name

I am migrating a jet database to ms-sql, the application is well established and contains tens of thousands of lines of code and possibly well over a 100 thousand line of code.
When using adodb recordsets (in vb.net) to query a jet database and joining tables that have columns with the same name (select a.foo, b.foo), you get TableName.Column returned for those fields that have the same name.
But when querying a sql server (MS SQL), you get the ColumnNames only so if you have two tables a and b and with each having a column with the name foo, you would get two columns both named "foo" where as with the jet database you get a.foo and b.foo
I am fully aware that you can use "as" in your select statement to alias the column name like (select a.foo as a_foo, b.foo as b_foo) but
I don't want to do that - I don't have time to go through the thousand or so of select statements in the app.
So I am asking if anyone knows of a way that SQL would be able to behave like a jet database and return the TableName.ColumnName for fields with similar name,
or if someone knows for sure that mssql can only return ColumnName without TableName, then which database engine can do what Jet database does?
By the way, the Jet database does that only when using a direct connection string syntax, otherwise it would behave the same way as SQL Server if accessing the data via ODBC.

How to join 2 tables from different databasess

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.

Copy data from SQL Server to Oracle

I have to copy data from several tables from SQL Server to Oracle. The tables have the same names on both DBs and the total number of rows to be copied is aboput 300 records. So some INSERT statements will be enough.
I tried using SSMS Tools. It generated me scripts with INSERT statements but the execution on Oracle fails because of the UNION clause (ORA-00923: FROM keyword not found).
Can someone recommand me another easy way to copy the data.
Thanx in advance
INSERT INTO tbl SELECT x UNION SELECT y UNION SELECT z isn't valid Oracle not because of the UNION, but because of the SELECTs.
You could simply change it to INSERT INTO tbl SELECT x FROM DUAL UNION SELECT y FROM DUAL UNION SELECT z FROM DUAL
You could quickly do this manually with a search and replace UNION with DUAL UNION and add one DUAL on the end.
To convert the Microsoft SQL Server database to Oracle, you need to create a repository to store the required repository tables and PL/SQL packages.
Have a look at this article
http://st-curriculum.oracle.com/obe/db/hol08/sqldev_migration/mssqlserver/migrate_microsoft_sqlserver_otn.htm
You also need to create database capture scripts as in here
http://st-curriculum.oracle.com/obe/db/hol08/sqldev_migration/mssqlserver/viewlets/sqlserver_capture.swf
These are in sqlserver.ocp format
There is a copy feature in SQL Server 4.1 - which does copy table from SQL Server to Oracle. For no so big tables, it works fine however, for larger tables you may get some errors.
This is in case of Migration of data from SQL server to Oracle. However, the concern here is just for getting copied sql server database to Oracle.
The question is about "NOT converting" - it is just copying the sql server database objects table to oracle. If you go to Tools Menu of the recent SQL Developer to you will see "database copy" however there is also a another feature "migration" on the same sql developer 4.1's tools menu
So Oracle SQL developer has two separate features - 1. Data Migration 2. Data copy
Look the sql developer's manual - you are talking Data Migration feature and question is all about "Copy"of table or tables data.

How do I create a named query to join multiple data sources in SSAS 2005?

In the SQL Server 2005 books online section "Defining Named Queries in a Data Source View (Analysis Services)", it states:
A named query can also be used to join multiple database tables from one or more data sources into a single data source view table.
Does anyone know where I can find examples or tutorials on how this can be done?
EDIT: To provide some additional background...
I am working with an analysis services project in the SQL Server Business Intelligence Development Studio for SQL Server 2005. I have defined a data source for each of my databases which are on different servers. I am trying to create a named query which will be a union of a table from each data source. The problem is that the named query requires me to choose a single data source for the query. The query is executed against this data source which does not know anything about the data sources in my project. However, according to the SQL Server 2005 books online, what I am trying to accomplish should be possible based on my quote from above.
MSDN has this link describing Named Queries and this link walking you through the process of creating one.
Edit: I think that to use multiple datasources, you would need to fully qualify your table to hit other datasources when creating your query, like this:
SELECT user_id, first_name, 'DB1' as DB FROM users
UNION
SELECT user_id, first_name, 'DB2' as DB FROM Database2Name.dbo.users
to get results like
user_id first_name DB
1 Bob DB1
2 Joe DB1
11 Greg DB2
12 Mark DB2
If by "multiple data sources" you mean multiple databases, then you can do this if you fully qualify the database name.
For example if I have two databases I can do this:
SELECT * FROM DatabaseA.dbo.SomeTable
JOIN DatabaseB.dbo.OtherTable
ON DatabaseA.dbo.SomeTable.Id = DatabaseB.dbo.OtherTable.Id
Make sure that you don't forget the dbo bit (the owner), otherwise it won't work.
The only other sort of "multiple data sources" that I'm aware of is distributed queries which allows you to perform queries over multiple remote instances of sql server:
sp_addlinkedserver 'server\instance'
SELECT * FROM [server\instance].DatabaseA.dbo.SomeTable
JOIN DatabaseB.dbo.OtherTable
ON [server\instance].DatabaseA.dbo.SomeTable.Id = DatabaseB.dbo.OtherTable.Id

Resources