How to perform cross-server queries from Azure Cloud Database to On Premise Server Database - sql

Goal
To create a query from an Azure server SQL Database to an on premise server server database.
This query used to be made using linked server objects from two on premise servers with their respective databases.
I have already successfully done cross database queries between two databases on the Azure cloud server. However, I have not been able to replicate the similar aspect that a linked server object can provide between an Azure server and an on premise server.
Current Scenario
On serverA I have created a linked server object to serverB. My two on premise servers communicate as such below:
--serverA = on premise server
--serverB = on premise server
Using mycn As New SqlConnection("Data Source=serverA;Initial Catalog=DatabaseA;User Id=username;Password=pwd")
Dim query As String = "SELECT * FROM [DatabaseA].dbo.tableA " &
"INNER JOIN [serverB].[DatabaseB].dbo.tableB ON tableA_ID = tableB_ID"
End Using
External Data Source
In order to communicate with my on premise server to my Azure SQL server I must create an external data source... I believe my problem relies in my external data source.
-- ===========================================================
-- Create external data source template for Azure SQL Database
-- ===========================================================
IF EXISTS (
SELECT *
FROM sys.external_data_sources
WHERE name = N'serverB_DataSource'
)
DROP EXTERNAL DATA SOURCE serverB_DataSource
GO
CREATE EXTERNAL DATA SOURCE serverB_DataSource WITH
(
TYPE = RDBMS,
LOCATION = N'serverB',
DATABASE_NAME = N'databaseB',
CREDENTIAL = myCreds
)
GO
Since I am trying to access my on premise server called serverB from the Azure server, do I need to specify it's actual IP? Not sure what I'm missing here...

Since I am trying to access my on premise server called serverB from the Azure server
you can't do this.. but you can do other way around
Lets say your azure server name is AZ and onpremises server name is B... you can create a linked server for AZ in B and query AZ from B

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)

Error during SELECT sql query between Azure SQL DBs

I have .NET Core app with EF Core.
I use Azure SQL DBs.
I have one main DB "Catalog" and multiple others.
I'm trying to populate one table in multiple DBs but value I need to get from main DB "Catalog".
I did the following migration:
migrationBuilder.Sql("INSERT INTO [dbo].[Table2] "
+ "(Name, Description)"
+ " VALUES "
+ "((SELECT param1 FROM [Catalog].[dbo].[Table1] WHERE DB_NAME() = CONCAT(param1, '-', param2)), 'some description')");
But in result I see:
Reference to database and/or server name in 'Catalog.dbo.Table1'
is not supported in this version of SQL Server.
So I can't execute SELECT queries between Azure DBs? Or my sql is wrong? How can I resolve this issue?
If you want to query remote database, you can use the Azure SQL Database elastic query.
For example
create a certificate for the remote database
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<master_key_password>';
CREATE DATABASE SCOPED CREDENTIAL RemoteDBCred
WITH IDENTITY = '<username>',
SECRET = '<password>';
Create an external data source fro the remote database
CREATE EXTERNAL DATA SOURCE MyRemoteDBSource WITH
(TYPE = RDBMS,
LOCATION = '<server_name>.database.windows.net',
DATABASE_NAME = '<>',
CREDENTIAL = RemoteDBCred,
) ;
Create an external table with the data source
CREATE EXTERNAL TABLE [dbo].[<>]
(
)
WITH
( DATA_SOURCE = MyRemoteDBSource)
Query the table
For more details, please refer to the document and the blog

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.

migrating multiple linked server from one server to another on sql server 2008 R2

I have 25 linked server(which will connect remote sql server 2000) working perfectly on one server, i even don't know login information of those linked server. My current environment is sql server 2008 R2, my new environment is also sql server 2008 R2 , i just need to migrate all linked server from old to new, preserving all login information
Possible?
If you're dealing with linked servers that have specific users specified with password then I don't think you'll be able to preserve that password information. Since linked servers are at the server level that information wouldn't be in any db backup. But, you could run queries like the one below to get the list of linked servers and check which ones use pass-through credentials. For the ones that do, just create new linked server definitions in the new database (you can easily get this SQL by right-clicking on the linked server in SSMS and generating the script). For the ones that don't, you can create the linked server via TSQL but you'll have to give them new credentials.
SELECT
serv.NAME,
serv.product,
serv.provider,
serv.data_source,
serv.catalog,
prin.name,
ls_logins.uses_self_credential,
ls_logins.remote_name
FROM
sys.servers AS serv
LEFT JOIN sys.linked_logins AS ls_logins
ON serv.server_id = ls_logins.server_id
LEFT JOIN sys.server_principals AS prin
ON ls_logins.local_principal_id = prin.principal_id

Link Server using mirroring

I need to configure a SQL Server 2008R2 link server that goes to one database which is principal and has a mirror failover database. For example I'm in server A that needs to perform a query in server B and in case of failover goes to server C. I create a link server named MyLinkS
I'm trying to configure the link server using this connection string:
Server=B;Failover Partner=C;Initial Catalog=MyDatabase;Integrated Security=True;
When I'm in server A I'm doing this query:
Select Name from [MyLinkS].[MyDatabase].[dbo].[MyTable] where Name = 'Jhon'
When Server B is Principal, the query works, but when I change to be the mirror, it doesn't work, it said that "It is in the middle of a restore".
How can I use the same query as before using one or the other server depending if B is principal or mirror?
Thank you
Maybe it will work for you:
Create SYNONYM for table on server B.
Create another SYNONYM for table on server C.
Use TRY/CATCH for your select - select from B in TRY block and select from C in CATCH block.