How can I query two separate non-linked SQL Servers? - sql-server

My goal is to pull data from 5 tables into one resultset, using a UNION Query.
Problem is that my tables are distributed across two separate servers (SQL Server v11.0 and SQL Server v13.0). They are not linked, they cannot be linked, and they have no relationship whatsoever.
Is there anyway to do that?

That is not going to happen in 11 and 13. But if it's really just a union you need, import to a staging area using bcp or your favorite ETL tool; if staging is in one or the other servers then you can union right there and save the transfer of any duplicates that would have been removed from the union (assuming we want a union and not union all).

You can try througt ACCESS. You can connect ACCESS with ODBC to both server, do a union in ACCESS and then upload the result where you want.
But this depend on the dimensions of the table of course.

Related

SSIS union all vs sql server Union All

I use SQL Server 2012 and SSIS. I have two Tables in the same server and same database. I need to transfer all records of both tables into third table.
I need to add some columns (Like Execution ID and some Package Parameters) to result of UNION ALL and after that I have to transfer the records into third table.
I have two solution for doing that but I don't know which ones is more efficient.
Solution 1 : Use two OLE DB DataSources and use Union All Component in SSIS
Solution 2 : Use Union All in SQL Server side and use just one OLE DB Source in SSIS.
Which one is more efficient?
Always favor database operations when possible. If 2 tables are in the same database, there is absolutely no reason to favor an SSIS operation over the query optimizer.
Union All is a no blocking operation, so there will be almost no difference in this case, but if this was a join or a more complex operation then the query optimizer would come into play.
Use the database solution as a rule of thumb.
You can also consider doing it in simple Execute SQL task.
Insert into T3
select * from T1
union all
select * from T2
Sorry to disagree with Cafe Con Leche but once you are comfortable with SSIS you should always use an SSIS task over TSQL. Except for sort operations SSIS is usually faster and provides very easy to set up error handling (including 'bad' rows being redirected to be processed/fixed later) and logging. Almost anything that can be done in SSIS can be done in TSQL (especially if you are comfortable using sp_cmdshell) but its much harder to do logging and error handling in TSQL that is built into SSIS.

best practice for large remote tsql query

I have a rather large query that builds three tables and union all's them together. All of the tables used for this are remote, the output is used locally.
Would I see a performance gain by creating a view on the remote server with my union query, then selecting from that, rather than have a lot of remote calls from my local query?

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 query tables located in different database?

My original question was about whether to keep separate ASPNETDB.MDF from the application database or merge all the tables in one database. Checking the previous questions/answers, I learned that it depends on whether the membership data would be shared across several applications.
Now, my question is this. In case I decide to keep ASPNETDB.MDF separate from the application DB, how can I query 2 tables located in 2 different databases?
Thanks for helping.
If you have two databases/schemas on the same database server, you can query across databases with the following syntax:
select *
from database1.dbo.table1 t1 join database2.dbo.table2 t2 on
t1.field1 = t2.field2
If they are on physically separate servers, you can still do a cross-database query, but you need to link the servers first:
http://msdn.microsoft.com/en-us/library/aa213778(v=sql.80).aspx
You can check your SQL Server version by:
SELECT SERVERPROPERTY('Edition')
If you are using "SQL Azure" you will not be able to use a table from different database. You will get this error:
Reference to database and/or server name in 'DataBase.Schema.Table' is not supported in this version of SQL Server.
Even if you try to Query a different database from your file like this:
USE ANOTHER_DATABASE;
You will get this error:
USE statement is not supported to switch between databases. Use a new connection to connect to a different database.

How do I join two tables from two different databases?

Is there any way to use a query and join two tables that is in two different database on the same server for DbVisualizer? I used the following for the SQL server
Select * from table union select * from datbase.dbo.table2
I tried this for the DbVisualizer, and it didnt work. How do I do this?
If the databases are in different servers you need to make sure that they are set up as linked servers.
Also be warned that the optimizer is relatively weak in this scenario, same server or not. The problem is that the statistics used for weighting costs of different operations aren't necessarily meaningful between different databases, especially at the point where the two databases will "intersect". So performance isn't what it could be.
If DBVisualizer supports views, manually setup a view of table2 in your database.
create view table2 as select * from database.dbo.table2
I dont think it can be done. I resolved the situation, by running a nightly data transfer to the SQL server. I do the union select from there...

Resources