How do I join two tables from two different databases? - database

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...

Related

Alias a database name within the query as to avoid dynamic SQL?

We have 4 distinct environments for one of our applications through our Dev to Prod pipeline, each having two databases. Within these databases there are several queries referencing the other database. So for example we may have this:
select a.name, b.description
FROM Database1.dbo.Names a
INNER join Database2.dbo.Descriptions b on a.ID = b.ID;
Just an example, not an actual query from our system... but hopefully you get the point. All four environments live on their own SQL servers so I can gracefully move code from bottom to top without any changes.
Now we're looking at possibly combining the bottom three environments on to the same SQL Server Instance (Prod will keep its own server), so all six databases will be on the same server together. In doing this the database names will have a suffix of their type, so Database1_Dev and Database2_Dev for example.
Well there lies the issue, all queries using just Database1 and Database2 will now break, and if I do change them to use Database1_Dev and Database2_Dev at the lowest level I can't just move them up to the next level Database1_Sandbox and Database2_Sandbox without modifying the queries.
The options I'm looking at are requesting three SQL instances on this server - which I'm sure will get shot down - or changing the queries to use dynamic SQL where I can pass in the DB name as a variable. I don't like either option.
Synonyms are the only thing I can think of that would allow a different name to be used, but this is going the wrong direction. I don't need one DB to have multiple names, rather multiple databases to have the same name within the same context. So like if in a procedure I could pass in the environment and I could alias Database1 to Database1_Sandbox just within the transaction or query. I don't think there is any way to do this, but I thought I'd ask.
Thanks for any suggestions.

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

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.

Cross database queries.How to proper use cross database features?

I am investigating the possibility to split one DB into multiple. We decided to move some tables into another database, but we have queries with join on these tables. I found a few solutions about how to achieve that:
Azure SQL Database elastic query
EXTERNAL DATA SOURCE
But I don`t know what the difference between them and what to choose.
Thanks for any help!
Azure SQL Database Elastic Queries and External data sources are two names for the same concept.
My suggestion is to avoid cross database queries and avoid splitting one database into multiples because query performance involving external data sources won't be the same no matter what strategy you choose to query those external tables.
If you still want to stick with the plan of splitting the database into multiple databases, then know that cross database queries show good performance when the remote tables are not big. When remote tables are big, this article shows you how to perform joins remotely using table variables and improve performance. This other article shows you also how to push parameterized operations to remote databases and improve performance.
if you are thinking to split your DB into multiple SQL server DB with the different host then you can prefer Linked server which has flexible to join across SQL servers

Is there a way to automatically join many tables in SQL Server?

I just imported ~50 tables; each table has 2 common foreign keys (making each record unique). My goal is to setup a query that joins all these tables; I obviously don't want to have to this manually and was thinking about setting up a procedure that loops through all tables to dynamically build this query ... is this the best way or is there an obvious solution I'm not seeing? Thanks you
No there is not. You have to manually write the query to JOIN the tables.
You can also check Automatically Generate a Set of Join Filters Between Merge Articles (SQL Server Management Studio) but I am not sure if that is going to help.

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.

Resources