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

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]

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.

replication multiple databases into one database

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

How to select a table from one machine to another using sql server 2008 r2?

I have explained the scenario below:
I am having two servers:
server 1
server 2
In both the servers I have Sql Server 2008 r2.
In server 1, I have a database named "DB_Server1" and in server 2, a database named "DB_Server2".
In DB_server1 database, I have a table named "TB_Server1" and in DB_Server2, a table named "TB_Server2".
My requirement is, in DB_Server1, I'm going to write a stored procedure which selects the table "TB_Server2" from DB_Server2 which is located in the server 2.
How can I achieve the above requirement?
you can use linkserver
at first select Server1
step 1: exec sp_addlinkedserver 'Server2'
step 2: select * from [Server2].[DB_Server2].TB_Server2
You can create linked server (http://msdn.microsoft.com/library/ff772782.aspx)
Use Linked Server:
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.
Here is a simple tutorial about how to create a linked server. After creating linked server, we can query it as follows:
select * from LinkedServerName.DatabaseName.dbo.TableName
Click here for another tutorial.
Read more about Linked Servers here.

Sql: export database using TSQL

I have database connection to database DB1. The only thing I could do - execute any t-sql statements including using stored procedures. I want to export the specific table (or even the specific rows of specific table) to my local database. As you can read abve, DBs are on diffrent servers meaning no direct connection is possible. Therefore question: Is it possible to write query that returns the other query to execute on local server and get data? Also note, that table contains BLOBs. Thanks.
If you have SQL Server Management Studio, you can use the data import function on your local database to get the data. It works as long as you have Read/Select access on the tables you are trying to copy.
If you have Visual Studio you can use the database tools in there to move data between two servers as long as you can connect to both from your workstation.
Needs Ultimate or Premium though:
http://msdn.microsoft.com/en-us/library/dd193261.aspx
RedGate has some usefull tools too:
http://www.red-gate.com/products/sql-development/sql-compare/features
Maybe you should ask at https://dba.stackexchange.com/ instead.
If you can login to the remote db (where you can only issue t-sql), you may create linked server on your local server to the remote and use it later directly in queries, like:
select * from [LinkedServerName].[DatabaseName].[SchemaName].[TableName]