Query tables between two different servers in SQL Server - sql

I have two databases, one in SQL Server and one in Azure SQL Synapse. Now I want to have one single query that should be done between two tables from these different servers.
For example:
SQL Server name : server1
Database name : db1
Table name : tb1
Azure Synapse name : prod-synapse-2
Database name : db2
Table name : tb2
Now query should be like this:
select
tb1.col1, tb1.col2, tb2.col3, tb2.col4
from
tb1
outer join
tb2 on tb1.col5 = tb2.col5
The above query is very simple to join two tables from the same database or two tables from the 2 different database within same server.
But I want suggestions to have the above query between 2 different servers.
I have seen in many stackoverflow questions similar to this, they suggested an answer using Linked Server, but that cannot be done in my case, because I will not be provided with access to create Linked server, and link the 2 servers in Microsoft SQL Server Management Studio.
Thanks in advance

Another way to query a database hosted in a remote SQL Server is the OPENROWSET T-SQL function. To use the OPENROWSET ad hoc method, you need to provide all connection information that is required to connect to the remote SQL server and many other resources.
Using OPENROWSET requires enabling the Ad Hoc Distributed Queries advanced configuration option same as the OPENDATASOURCE function.
You need to provide the provider’s name, the connection string and the query as follows:
OPENROWSET(‘providername’,’datascource,’query)

Related

How to copy one table data to another one between two Azure databases on the same server

I want to copy data from one database table into another database table on the same server in Azure SQL. I have done all of the Azure SQL Cross Database Query' steps that are written here https://www.mssqltips.com/sqlservertip/6445/azure-sql-cross-database-query/but still get the same error whenever I execute a query
'Reference to database and/or server name in 'db name' is not supported in this version of SQL Server.'
Can you pls help to figure out this?
Azure SQL database doesn't support across query directly.
We can not use USE statements and it not supported. That's why you get the error. We can not run statements like select * from [other_database].[schema].[table].
In Azure SQL database, only elastic query overview (preview) can achieve 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.
You could follow the tutorial and it may be more complex than on-premise SQL Server:
Get started with cross-database queries (vertical partitioning) (preview)

Best way to update one SQL Server with data from another SQL Server?

I would like to run a job that automatically snapshots a DB1 (a stored procedure) and merges the result with a table in DB2. Basically I would like to query DB1 from DB2.
What is the best way to do this? They are run on two different SQL Servers in two different resource groups in Azure.
At the moment it won't let me created a linked server - tells me the sp does not exist when I try create it.
You have two databases run on two different SQL Servers in two different resource groups in Azure.
Basically you would like to query DB1 from DB2.
For Azure SQL database, you want to query across different database, you can use Azure SQL Database elastic query.
Summary:
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. Using this feature, you can scale out queries to large data tiers in SQL Database and visualize the results in business intelligence (BI) reports.
You can using the bellow code to query from the remote database:
EXEC sp_execute_remote
N'MyExtSrc',
N'select count(w_id) as foo from warehouse'
For more details, please see: Stored procedure for remote T-SQL execution: sp_execute_remote.
You can also reference this blog: Is it Possible to call Functions and Stored Procedures of One database In another database- Azure Sql server.
With elastic query, you can call the stored procedure in DB1, merges the result with a table in DB2
Hope this helps.

Ways to access table data from different servers

I need the data for reportviewer in .NET However the final report involves inner join between different tables in different databasea and different servers. I am currently using XML and dynamic SQL to centralize my code query. What other better methods can i use other than creating multiple similar stored procedures or linking servers (new servers will be added anytime and mapped in a table in one of the server database)
In your main server that contains a table that has your databases information you can use dynamic SQL with one of this functions:
OPENDATASOURCE (Transact-SQL)
Provides ad hoc connection information as part of a four-part object name without using a linked server name.
OPENDATASOURCE ( provider_name, init_string )
Example:
The following example creates an ad hoc connection to the Payroll instance of SQL Server on server London, and queries the AdventureWorks2008R2.HumanResources.Employee table. (Use SQLNCLI and SQL Server will redirect to the latest version of SQL Server Native Client OLE DB Provider.)
SELECT *
FROM OPENDATASOURCE('SQLNCLI',
'Data Source=London\Payroll;Integrated Security=SSPI')
.AdventureWorks2008R2.HumanResources.Employee
or
OPENROWSET (Transact-SQL)
Includes all connection information that is required to access remote data from an OLE DB data source. This method is an alternative to accessing tables in a linked server and is a one-time, ad hoc method of connecting and accessing remote data by using OLE DB.

SSRS report builder 2.0 How to link 2 servers and run in one dataset

I am trying to find a way of linking data from two servers in SSRS report builder 2.0
ultimately using one dataset to query 2 databases in 2 different servers.
The SQL that i have is very basic as shown below and i can run the query easily in SQL management studio.
select * from
[server1].[DES].[dbo].[Daily] dr
LEFT JOIN [server2].[VES].[dbo].[Rou] mr
ON dr.ID = mr.id
Also the access i have to the reporting server is read only so i cant really make any administrative changes to the configuration.
I have explored the connection string to create a connection but it only allows me to connect to one server for each dataset. what i am trying to do is use one dataset
Any help will be greatly appreciated as i am a junior in SQL and SSRS
Using SSRS and MS SQL 2008 R2
if you executing this query from your SSMS it means this other server
is a linked server. then it should be no different to execute it from
SSRS.
Yes it is right you can only add one server to your data source of
your dataset whether its a shared data source or embedded.
But for instance if you have a data source pointing to say Server A
when you executing queries which will be pulling data from Server A
and also from server B you will Use fully Qualified name for the
Objects from server B and two part name from server A.
something like this ...
SELECT *
FROM Schema.Table_Name A INNER JOIN [ServerNameB].DataBase.Schema.TableName B
ON A.ColumnA = B.ColumnA
obviously ServerB has to be a Linked Server.
I can think of two possible solutions
Option 1:
Creating a stored procedure on server1 (With a query accessing the another server). And call this stored procedure from SSRS (Dataset).
Option 2:
Create two dataset with two different connection strings on the RDL (one for Server1 and another for Server2).
Use the "LookUpDataSet" function in Report Builder to merge the results using the key fields.
Option 2 is preferable.

How can I select data in the same query from two different servers and databases from SQL Server Management Studio?

How can I select data in the same query from two different databases that are on two different servers, one DB2 Server and the other a SQL Server?
On your sql server, set up a linked server to the db2 database.
Then write your query on sql server. I suggest that you use openquery for the db2 stuff. If you have to combine the data, populate a sql server temp table with the openquery results and work from there.
The reason I suggest this is performance. I have found that if you use this syntax
select somefields
from server.database.owner.table
where whatever
sql server will bring back the entire table from the linked server and apply the where clause afterwards.
You can set up a linked server http://support.microsoft.com/kb/222937
How to create a linked server