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

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.

Related

Query tables between two different servers in SQL Server

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)

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)

Making ONE single JOB for 2 Separate SQL Agents running on SQL2008 and SQL 2016

Scenario:
2 servers - SQL2008 and SQL2016
Job 1 : Backup - Scheduled SQL Agent Job running for 2 Databases on SQL2008 with the destination on SQL2016 server
Job 2 : Restore - Scheduled SQL Agent Job running for 2 Databases on SQL2016 from the that location.
So thinking of making these 2 individual into a single job for Backup and Restore.
Please help/guide me to combining these 2 Jobs.
Do I Use Powershell or SQL for getting this correct?
When communicating between two servers, the best way I know is to create a Linked Server going both-ways, and on each server. The only gotcha is to make sure the SQL syntax used to select or insert data from a server, is compliant with that server, or the linked server.
From SQLShack...
"Linked servers allow submitting a T-SQL statement on a SQL Server instance, which returns data from other SQL Server instances. A linked server allows joining data from several SQL Server instances using a single T-SQL statement when data exists on multiple databases on different SQL instances. By using a linked server to retrieve data from several SQL instances, the only thing that should be done is to connect to one SQL instance.
There are two ways of configuring linked server in SSMS. One way is by using sp_addlinkedserver system stored procedure and another is by using SQL Server Management Studio (SSMS) GUI interface."
Without writing out the SQL code for both backups and restores, I suggest you read up on Linked Servers, and apply them into your code. I recommend you test the code first manually, outside of a job, and then apply it to a scheduled job or Maintenance Plan.
But as a hint :), try the syntax when linking your table from a linked server object:
[LINKED SERVER].[DATABASE].[Schema].[TableName]

How to compare the data between two query results from SQL Server and DB2

As part of a migration project I need to test the data in the source database (in SQL Server) with the target database (in DB2).
Some of the columns from the source are mapped to the target tables. The source database has millions of rows and I need to confirm the data is properly migrated.
How can I compare the data resulting from SQL queries on both the source and target databases?
Please provide some solution.
In MS SQL Server you can link to DB2 using the Linked Server feature, then you can execute some queries against both servers or even joining them in a same query. Be aware of the cost (network, disks, memory...). You will have to perform some performance tests.
See how to link the servers:
Creating a linked server to DB2 using Microsoft OLE DB provider for DB2
As #Caffe said, you can link both databases. From DB2 you can do that by Federation. However, this option is not included in the basic installation, and you should use Information Integration (that is not the last name, it changed recently).

Query a Linked Server with Excel Connection

In the past, I have been querying various different databases (let's call them DB1, DB2, and DB3) by using Data->From Other Sources->From SQL Server in Excel. However, I want to be able to perform just one query (say, DBMaster) and using this to populate my data.
My thoughts: Use linked servers. I create DBMaster with linked servers to DB1, DB2, and DB3. However, I do not know how to access those individual databases from Excel. When I connect to DBMaster, I only get the tables on that database, not the tables on the linked servers. Does anyone know how I can do this?
Thank you!
You just need to connect your master database and exceute your query from there.
Just remember to put the server name before database name in your sql in FROM clause like :
[SERVER].[DATABASE].DBO.[TABLE_NAME]