英文:
Not able to query cross database in Azure SQL
问题 {#heading}
我尝试将数据插入到另一个数据库的另一个表中,但我遇到了一个错误:
> 在此版本的SQL Server中,对于数据库和/或服务器名称 'xxx.xxx.xxx' 的引用不受支持。
当尝试从另一个数据库中进行选择时,我收到相同的错误消息。
我的SQL代码:
INSERT INTO xxx.xxx.xxx
VALUES (xx,'xx', 'xxxx', xx)
英文:
I tried to insert data into another table, in another database, but I get an error:
> Reference to database and/or server name in 'xxx.xxx.xxx' is not supported in this version of SQL Server
I'm getting the same error when trying to select from another database.
My SQL code:
INSERT INTO xxx.xxx.xxx
VALUES (xx,'xx', 'xxxx', xx)
答案1 {#1}
得分: 1
在Azure SQL数据库中,不支持跨数据库查询。您无法从一个数据库向另一个数据库插入记录。
您不能在Azure SQL中对跨数据库使用DML语句。
- 唯一的插入记录的方法是连接到正确的数据库,或者如果您在SSMS/ADS/门户中,则切换数据库上下文,然后执行您的语句。
- 关于跨数据库查询,您需要使用弹性查询,例如外部表和外部数据源,建议由Alberto Morillo提供。
示例代码:
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'password';
CREATE DATABASE SCOPED CREDENTIAL samplecred2
WITH IDENTITY = 'username',
SECRET = 'password';
CREATE EXTERNAL DATA SOURCE RemoteReferenceData
WITH
(
TYPE=RDBMS,
LOCATION='servername.database.windows.net',
DATABASE_NAME='db name',
CREDENTIAL= samplecred
);
CREATE EXTERNAL TABLE [dbo].[tableename]
(
Id int,
Name varchar(20)
)
WITH
(
DATA_SOURCE = RemoteReferenceData,
SCHEMA_NAME = 'schemaname',
OBJECT_NAME = 'tablename'
);
现在您可以在外部表上执行类似的查询。
参考链接 - Azure SQL数据库中的跨数据库查询 - Microsoft社区中心 英文:
In Azure SQL database Cross database quires are not supported You cannot insert records from one database to another database.
You cannot use DML statements on Cross database in azure SQL.
- The only way to insert records is connect to the correct database or switch the database context if you're in SSMS/ADS/ portal Then you can execute your statements.
- Regarding the cross database quires, you need to use elastic queries like external tables and external data sources suggested by Alberto Morillo
Sample Code:
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'password';
CREATE DATABASE SCOPED CREDENTIAL samplecred2
WITH IDENTITY = 'username',
SECRET = 'password';
CREATE EXTERNAL DATA SOURCE RemoteReferenceData
WITH
(
TYPE=RDBMS,
LOCATION='servername.database.windows.net',
DATABASE_NAME='db name',
CREDENTIAL= samplecred
);
CREATE EXTERNAL TABLE [dbo].[tableename]
(
Id int,
Name varchar(20)
)
WITH
(
DATA_SOURCE = RemoteReferenceData,
SCHEMA_NAME = 'schemaname',
OBJECT_NAME = 'tablename'
);
Now you can execute quires on the external table which looks similar.
Reference -Cross-database Query in Azure SQL Database - Microsoft Community Hub