Querying multiple database systems - sql

Is it possible to query (or better, join) data from two different database systems? Let's say I have postgresql and SQL Server and I want to join a table from postgres to a table in SQL Server?
It doesn't matter which programming language.

What about utilizing a linked server? I use one to query an AS400 and join the results back to Microsoft SQL Server.
Quote from Microsoft documentation:
Configure a linked server to enable the SQL Server Database Engine to
execute commands against OLE DB data sources outside of the instance
of SQL Server. Typically linked servers are configured to enable the
Database Engine to execute a Transact-SQL statement that includes
tables in another instance of SQL Server, or another database product
such as Oracle. Many types OLE DB data sources can be configured as
linked servers, including Microsoft Access and Excel. Linked servers
offer the following advantages:
The ability to access data from outside of SQL Server.
The ability to issue distributed queries, updates, commands, and transactions on heterogeneous data sources across the enterprise.
The ability to address diverse data sources similarly.

Related

How can I join tables from different databases in Azure SQL (SAAS)?

Having the older application that joins SQL tables from different databases like this:
SELECT a.value, b.value
FROM databaseA.dbo.tableA AS a
JOIN databaseB.dbo.tableB AS b
ON a.thekey == b.thekey
Being 3rd party, we have to accept the decision of the main implementor for the customer to use the license Azure SQL SAAS (Software As A Service; I am new to that, sorry if some terms are incorrect).
Is it possible to have databaseA and databaseB (that part is possible, checked through SSMS) in the Azure SQL and do the same JOIN (that part is unknown to me)?
I tried, but it failed. I do not know if something more have to be done to make the other database visible from each of the databases, or if it is not possible at all.
If it is not possible, what is the recommended technique to replace the old SQL code?
As you comment said: "I understand that it is different than working within one physical SQL Server. On the other hand, the different servers is probably related to possible replication. Is that correct? ", yes, you're right.
For Azure SQL database(PSSA), we only can use the elastic query to achieve the cross database query:
The elastic query feature (in preview) enables you to run a
Transact-SQL query that spans multiple databases in Azure SQL
Database. It allows you to perform cross-database queries to access
remote tables, and to connect Microsoft and third-party tools (Excel,
Power BI, Tableau, etc.) to query across data tiers with multiple
databases.
Note:
Make sure the primary database have the permission to access the remote databases. Add the the client IP to their database firewall. The remote database can be in different Azure SQL Server.
If you are using Azure SQL managed instance and Azure SQL Server on Azure VM, you can run this across query like on-premise SQL Server.

Identify relationships between multiple databases in SQL

I am working on migrating an application from one server to the other. According to the connection string of this application, it is touching different databases. Meaning a view query in DB1 will touch a table in DB2. So while migrating this application, I constant get to see chain of 'Database unavailable' errors and every time I see such error, I have to migrate that specific database.
I am wondering, since we have ER diagrams to know about relationships between tables in a database, is there any way in SQL server to know the relationships/linkages between different DATABASES in a server? Are there any tool that does this?
Depending on number of databases you have, here would be a somehow quick way you can find that out (number of required search = number of available databases in the server):
Use 'SQL Search' application of Red-get
(https://www.red-gate.com/dynamic/products/sql-development/sql-search/download)
and search for the other database names one after another by selecting
your database of interest. Select all objects.
If you have metadata oriented design (a Stored Procedure looping through the names of different other Stored Procedures / Functions from different databases which are stored in a table as metadata and executing them with a wrapper Stored Procesure), then you will have make use of SQL Locator software (http://www.sqllocator.com/Downloads.html) to search for database names in SQL Table values.
Both of the above software are free.
You need to have SSMS (SQL Server Management Studio) installed to be able to use this application. After installation, ‘SQL Search’ will be directly available in your SSMS as an add-on.
SQL Locator can be directly used by providing the SQL Server name and your SQL Server credential.
Note:- The above steps will help you find out the referenced databases from a certain database within the same SQL Server. If you need to find out databases from Linked Server (I do not believe your question is asking that anyways), then you will have to smartly utilize the same above tools to find the external server reference by searching the external server name.

SQL synchronize database between local and remote server

I've got 2 companies in different locations and the main server is at my office. The main server is working with a full version of SQL Server where the companies use SQL Server express version.
What is the best way to synchronize these SQL Server databases together that the main server have always the latest updates?
There are several technologies specifically for this type of scenario:
SQL Replication
Supports unidirectional or bidirectional synchronization
SSIS
Lets you define the mappings of the data, as well as transformations, and attach other code to the process easily
Linked-servers
Allows you to query databases and tables on remote servers as though they are part of the local database. Very easy to setup (just call exec sp_addlinkedserver) and once defined uses nothing but plain old SQL
If you want to do in an event (like button click), I would suggest Linked Server. Click here for a simple tutorial about how to create a linked server. After creating linked server, we can query it as follows:
select * from LinkedServerName.DatabaseName.SchemaName.TableName

Can I connect two external databases using Microsoft Access?

First, I apologize if this has been asked or is a trivial question.
But, is it possible to connect to two external SQL databases through Microsoft Access?
By way of background one is our CRM and one is our email management system. The two are completely distinct databases. And, reside in different physical and virtual servers.
Our objective is to be able to gain insight into our email marketing efforts as it relates to client activities captured in our CRM.
We currently use an ODBC connection to read data from our CRM database.
Really, we need more information, but basically yes it is possible. All you need to do is link to the tables in the SQL databases, then run the query on the linked tables with a JOIN.
This page will tell you how to link to an SQL table: http://office.microsoft.com/en-au/access-help/import-or-link-to-sql-server-data-HA010200494.aspx
It is possible to do this in MS Access (as already said in Makita's answer).
But you won't get the maximum performance this way, because there's a certain overhead caused by MS Access on your machine (which is relatively slow compared to the DB servers and probably has a slower network connection than the DB servers), pulling data from two different DB servers over the network.
Since you tagged your question with sql-server, there's another possible solution using a feature of SQL Server: linked servers.
You can create a linked server in SQL Server A, that links to SQL Server B.
(see the above link for more information about how to do this)
Then, you can query tables on SQL Server B from SQL Server A (and even join tables from both servers) with the following syntax:
-- execute this on Server A
use DatabaseA
select *
from TableA a
inner join ServerB.DatabaseB.dbo.TableB b on a.ID = b.ID
(this code assumes that TableA is a table in DatabaseA, which is on ServerA, and so on)
This will be way faster than linking the tables in MS Access (the more data you need to pull, the faster it will be) because you're using the superior CPU power and network connection of the database servers.

Sql Azure Sync Dataset not supporting custom DataType of the tables while try to sync, is there any workaround for that?

The On-premises Database have tables and those tables columns types are UDTs, for this reason created same UDTs and tables using these UDTs on the cloud.
But when trying to syncing them its shows error not supporting UDTs, So am I missing something or is there any workaround to do syncing with UDTs?
Here if SQL Azure allows to create UDTs & also allows to create tables using those datatypes, then why not allow to sync?
FYI "SQL Azure" is called now "SQL Database" so if you find reference to Windows Azure "SQL Database" you can consider applied to your requirement as well.
SQL Database does not support user-defined data types, extended properties, Windows authentication, or the USE statement.
However the November 2010 update to SQL Server 2008 R2 includes support for SQL Database. The Generate Scripts Wizard now allows you to script for database version SQL Database so the scripts generated are directly compatible to be executed on SQL Database. The scripts thus generated are compatible with SQL Database and can be compiled on SQL Database without any further modifications. So your UDDT or custom data types can my transferred to SQL Database through this migration script.
If your Database is pre-SQL Server 2008, you can use the "Schema Migration with pre-SQL Server 2008 R2" section from this article.
unfortunately, there is no workaround at this time. The Data Sync Service is largely based on Sync Framework and it doesn't support UDT as well. The Data Sync Service supports spatial data types in the latest release though.