replication multiple databases into one database - sql

Before the question I want to explain what I'm trying to do.
I'm using SQL Server 2016 and I have 2 databases
DataBaseA
DataBaseB
In a remote server I created the same databases using the same creation SQL scripts that i used for DataBaseA and DatabaseB. Then I used the replication service from SQL Server 2016. My local database is the publisher and the remote server is the subscriber.
This worked fine.
What I want to do is having one database in the remote server that contains all tables from both databases and the 2 databases replicate to their corresponding tables.
For example
[DataBaseA].[dbo].[TableA] ---is Replicated into-->[AggregatedDataBase].[dbo].[TableA]
[DataBaseB].[dbo].[TableB] ---is Replicated into-->[AggregatedDataBase].[dbo].[TableB]
Is this possible? If yes how to do it else what other options to do what I'm looking for?

Yes,it is possible..two different publishers can have one subscriber..
If yes how to do it
Just set up the way you set up replication and while selecting subscriber choose aggregated database

Related

Query single server in SQL server group

I have 3 databases hosted across 3 different servers, all of which have different data and structures. I need to perform a query that will draw data from various tables across all three of them.
I've registered the three servers into a server group, and I've confirmed that all of the connections are working properly.
Here's an example of the problem I'm facing. For the first part of this query, I need to retrieve a list of records from the 'Applications' table in DB1 so I write:
SELECT * FROM [DB1].[dbo].[Application]
I know that this query works partially because it starts returning results from the correct table. The problem is that I haven't specified the server that DB1 is on, so once the query has finished querying DB1.dbo.Application, it looks for the same database and table on the next server. The database and table don't exist on the other servers so the query fails.
So how do I specify the server that I want the query to run on? I've tried [server_name].[DB1].[dbo].[Application], but it still runs the query across all of them.
Server groups are for maintenance purposes, what you need to use is a linked server
On one of the servers, say SERVER1, you will need to set up two linked servers - one to SERVER2 and one to SERVER3
From SERVER1 you will then be able to query the other servers using the four part name in a normal query window:
SELECT * FROM DatabaseName.dbo.Table1;
SELECT * FROM SERVER2.DatabaseName.dbo.Table2;
SELECT * FROM SERVER3.DatabaseName.dbo.Table3;
You can also use the tables from the remote server in JOINs etc as though they were on the local server and the remote servers don't even need to run SQL Server - they can be Oracle, MYSQL etc.
Be aware though, remote servers are slow and you may struggle with large datasets

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.

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]

OpenQuery or Create Duplicate Database on 2 different servers?

okay so I googled quite a bit and didn't find an answer, so here I am.
I have two servers, Lets say Server1 and Server2.
Server1 has 1 database S1.DB1.
Server 2 has 2 databases S2.DB1 and S2.DB2.
Now, I have to do reporting on Server 1 where I need to use data from S1.DB1 and S2.DB1. Should I be using OpenQuery to get the data from server 2 or should I just create a copy of S2.DB1 in Server 1 so I can easily access those tables in Server 1?
My basic concerns are speed of data extraction in case of OpenQuery and Storage issues in case of duplicating DBs. Which one is the right/better way to go about it?
Use OpenQuery if it's fast enough. If you have your DBA's cooperation, he could create a linked server, which is more convenient.
Replicating the data requires more administrative overhead and introduces failure modes. It can be helpful if the data changes slowly and are queried relatively frequently. (Use replication, though; don't just "copy the database".)

Data from 4 different database

I need to get data from 3 different databases on one event command. Can anybody tell me any efficient way besides I am querying to all three different database servers in a row:-
Server 1 : Select * from ....
Server 2 : Select * from.....
and so on...
Thanks very much
Seeing as the question is marked TSQL:
Install the providers for the 'other' databases.
In SQL Server 2005, create a linked server to each database, and then simply query as though the 'other' databases were SQL Server databases.
If the databases are on the same server instance, they can be queried in the format "database_name.table_name.column_name" otherwise I would use Mitch's answer (linked servers can be queried in the format "server_name.database_name.table_name.column_name")