Error during SELECT sql query between Azure SQL DBs - sql

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

Related

Column data transfer from server to server

I need to copy data from a particular column in a table and transfer to another table however, this involves moving from one SQL Server instance to another instance.
Does anyone have any tips for achieving this?
To transfer data from one instance to another you have 3 choices:
Using Linked Servers
Using SQL Server Integration Services
Using SQL Import and Export Wizards
You can link the 2 servers and use a query to insert the data.
To link the servers you can use the MSSQL procedures, like so:
EXEC sp_addlinkedserver #server='ServerName1', #srvproduct='', #provider='SQLNCLI', #datasrc='10.10.10.10'
EXEC sp_addlinkedsrvlogin 'ServerName1', 'false', NULL, 'username', 'password'
 
where ServerName1 is the name of the remote SQL server and #datasrc would be the ip address/dns name
To copy the data you can use a simple query like this one:
insert into [ServerName1].DatabaseName.dbo.TableName(column1,column2)
select Column1, null from LocalTableName
It's an example of course, since you didn't post the table setups. You would probably need to adjust it to suit your needs.

Azure SQL Data Migration Assistant (DMA) Error - Three or Four Part Names

I'm using the MS Data Migration Assistant tool to move a SQL Server 2016 DB to Azure. I'm getting the following error on 80+ stored procedures:
Queries or references using three- or four-part names not supported in Azure SQL Database. Three-part name format, [database_name].[schema_name].[object_name], is supported only when the database_name is the current database or the database_name is tempdb and the object_name starts with #.
All of these stored procedures are using the current database and referencing the current database name. For example, this instruction is causing the error:
DELETE FROM [STDR].[dbo].[report] WHERE [report_id] = #xid
and when I run the command:
SELECT DB_NAME();
I get:
STDR
Could this be an error in the DMA tool? It's preventing me from executing the migration. I'd rather not have to modify all of these procedures. Thanks.
1.Queries or references using three- or four-part names not supported in Azure SQL Database.
It's not the error in the DMA tool. Cross database queries using three or four part names is not supported in Azure SQL Server.
You can read more in the official documentation:Resolving Transact-SQL differences during migration to SQL Database。
2.Three-part name format, [database_name].[schema_name].[object_name], is supported only when the database_name is the current database or the database_name is tempdb and the object_name starts with #.
About this question, I have an idea and I think you can try it. You can specify target Azure Database instance which has the same database name and the same schema objects with your on-premises SQL Server. Otherwise, when your SQL Server 2016 DB is migrated to Azure, the current database is not [STDR] and cause the error.
Reference: Migrate on-premises SQL Server or SQL Server on Azure VMs to Azure SQL Database using the Data Migration Assistant.
Hope this helps.
It's just the four-part name or three-part name that is not compatible with Azure SQL Database. You can script all your programing objects and then change the three part name format to two-part name format (dbo.[NameOfTheObjet]) on the script using Find and Replace on a text editor like Notepad++, then run that script on your Azure SQL Database to migrate your programming objects.
After that you can use DMA only to migrate the schema and data of your tables.

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

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

Calling functions of master DB from other DB on the same Azure SQL Server

I have Azure SQL DB Server + one Azure SQL database on it. Within this DB I have functions which call some functions of master DB as a part of their logic.
Example:
CREATE FUNCTION [dbo].[EncryptByKey]
(
#EncryptionKeyId nvarchar(1024),
#ValueToEncrypt varchar(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
RETURN master.dbo.fn_varbintohexstr(ENCRYPTBYKEY(Key_GUID(#EncryptionKeyId), #ValueToEncrypt))
END
Gives me an error:
Cannot find either column "master" or the user-defined function or
aggregate "master.dbo.fn_varbintohexstr", or the name is ambiguous.
If instead I try:
exec master.dbo.fn_varbintohexstr(123)
The error I get is:
Reference to database and/or server name in
'master.dbo.fn_varbintohexstr' is not supported in this version of SQL
Server.
Are there any solutions on how to use master DB functions from user's DBs on Azure SQL server?
You cannot use distributed database queries using three or four part names on SQL Azure.
For queries that span multiple databases in SQL Azure, you need to use elastic queries. For more information, please visit this article.

Retrieving data from different servers and databases

I have two servers 10.10.7.10 and 10.10.2.10. 10.10.7.10 has the dev database with dbo as owner of a table named vendor. On 10.10.2.10 I have the same table in a database name prod. How do I retrieve data from both servers logging into 10.10.7.10 as a remote connection and using sql management studio to create and run my queries/
Create a linked server and use four part name from your dev server to retrieve data from your prod server.
Create a Linked Server
Logon to your Dev server and add the Prod server as your linked server using the following command,
EXEC master.dbo.sp_addlinkedserver #server = N'PRODSERVER'
, #srvproduct = N'SQLSERVER'
, #provider = N'SQLOLEDB'
, #datasrc = N'10.10.2.10'
The above statement will create a linked server to your Prod server 10.10.2.10 now you can write t-sql statement from your Dev server using the four-part name. something like....
Select * from [PRODSERVER].DBname.dbo.TableName
Create a Linked server on your Dev server called Prod.
Then you can run a query like this
SELECT p.*, dev.* FROM PROD.dbName.dbo.VENDOR as p
inner join dbo.Vendor as Dev
ON p.vendorId = dev.vendorId